home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / UTIL / PL 2.0 SupplementDoc Folder.sit / PL 2.0 SupplementDoc Folder / Documentation / Chapter 23. File System Interf… < prev    next >
Text File  |  1995-03-28  |  131KB  |  2,757 lines

  1. Common Lisp the Language, 2nd Edition
  2. -------------------------------------------------------------------------------
  3.  
  4. 23. File System Interface
  5.  
  6. A frequent use of streams is to communicate with a file system to which groups
  7. of data (files) can be written and from which files can be retrieved.
  8.  
  9. Common Lisp defines a standard interface for dealing with such a file system.
  10. This interface is designed to be simple and general enough to accommodate the
  11. facilities provided by ``typical'' operating system environments within which
  12. Common Lisp is likely to be implemented. The goal is to make Common Lisp
  13. programs that perform only simple operations on files reasonably portable.
  14.  
  15. To this end, Common Lisp assumes that files are named, that given a name one
  16. can construct a stream connected to a file of that name, and that the names can
  17. be fit into a certain canonical, implementation-independent form called a
  18. pathname.
  19.  
  20. Facilities are provided for manipulating pathnames, for creating streams
  21. connected to files, and for manipulating the file system through pathnames and
  22. streams.
  23.  
  24. -------------------------------------------------------------------------------
  25.  
  26.    *  File Names
  27.         o  Pathnames
  28.         o  Case Conventions
  29.         o  Structured Directories
  30.         o  Extended Wildcards
  31.         o  Logical Pathnames
  32.              +  Syntax of Logical Pathname Namestrings
  33.              +  Parsing of Logical Pathname Namestrings
  34.              +  Using Logical Pathnames
  35.              +  Examples of the Use of Logical Pathnames
  36.              +  Discussion of Logical Pathnames
  37.         o  Pathname Functions
  38.    *  Opening and Closing Files
  39.    *  Renaming, Deleting, and Other File Operations
  40.    *  Loading Files
  41.    *  Accessing Directories
  42.  
  43. -------------------------------------------------------------------------------
  44.  
  45. 23.1. File Names
  46.  
  47. Common Lisp programs need to use names to designate files. The main difficulty
  48. in dealing with names of files is that different file systems have different
  49. naming formats for files. For example, here is a table of several file systems
  50. (actually, operating systems that provide file systems) and what equivalent
  51. file names might look like for each one:
  52.  
  53. System          File Name
  54. ==================================================
  55. TOPS-20         <LISPIO>FORMAT.FASL.13
  56. TOPS-10         FORMAT.FAS[1,4]
  57. ITS             LISPIO;FORMAT FASL
  58. MULTICS         >udd>LispIO>format.fasl
  59. TENEX           <LISPIO>FORMAT.FASL;13
  60. VAX/VMS         [LISPIO]FORMAT.FAS;13
  61. UNIX            /usr/lispio/format.fasl
  62. ==================================================
  63.  
  64. It would be impossible for each program that deals with file names to know
  65. about each different file name format that exists; a new Common Lisp
  66. implementation might use a format different from any of its predecessors.
  67. Therefore, Common Lisp provides two ways to represent file names: namestrings,
  68. which are strings in the implementation-dependent form customary for the file
  69. system, and pathnames, which are special abstract data objects that represent
  70. file names in an implementation-independent way. Functions are provided to
  71. convert between these two representations, and all manipulations of files can
  72. be expressed in machine-independent terms by using pathnames.
  73.  
  74. In order to allow Common Lisp programs to operate in a network environment that
  75. may have more than one kind of file system, the pathname facility allows a file
  76. name to specify which file system is to be used. In this context, each file
  77. system is called a host, in keeping with the usual networking terminology.
  78.  
  79. [change_begin]
  80. Different hosts may use different notations for file names. Common Lisp allows
  81. customary notation to be used for each host, but also supports a system of
  82. logical pathnames that provides a standard framework for naming files in a
  83. portable manner (see section 23.1.5).
  84. [change_end]
  85.  
  86. -------------------------------------------------------------------------------
  87.  
  88.    *  Pathnames
  89.    *  Case Conventions
  90.    *  Structured Directories
  91.    *  Extended Wildcards
  92.    *  Logical Pathnames
  93.         o  Syntax of Logical Pathname Namestrings
  94.         o  Parsing of Logical Pathname Namestrings
  95.         o  Using Logical Pathnames
  96.         o  Examples of the Use of Logical Pathnames
  97.         o  Discussion of Logical Pathnames
  98.    *  Pathname Functions
  99.  
  100. -------------------------------------------------------------------------------
  101.  
  102. 23.1.1. Pathnames
  103.  
  104. All file systems dealt with by Common Lisp are forced into a common framework,
  105. in which files are named by a Lisp data object of type pathname.
  106.  
  107. A pathname always has six components, described below. These components are the
  108. common interface that allows programs to work the same way with different file
  109. systems; the mapping of the pathname components into the concepts peculiar to
  110. each file system is taken care of by the Common Lisp implementation.
  111.  
  112. host
  113.      The name of the file system on which the file resides.
  114.  
  115. device
  116.      Corresponds to the ``device'' or ``file structure'' concept in many host
  117.      file systems: the name of a (logical or physical) device containing files.
  118.  
  119. directory
  120.      Corresponds to the ``directory'' concept in many host file systems: the
  121.      name of a group of related files (typically those belonging to a single
  122.      user or project).
  123.  
  124. name
  125.      The name of a group of files that can be thought of as the ``same'' file.
  126.  
  127. type
  128.      Corresponds to the ``filetype'' or ``extension'' concept in many host file
  129.      systems; identifies the type of file. Files with the same names but
  130.      different types are usually related in some specific way, for instance,
  131.      one being a source file, another the compiled form of that source, and a
  132.      third the listing of error messages from the compiler.
  133.  
  134. version
  135.      Corresponds to the ``version number'' concept in many host file systems.
  136.      Typically this is a number that is incremented every time the file is
  137.      modified.
  138.  
  139. Note that a pathname is not necessarily the name of a specific file. Rather, it
  140. is a specification (possibly only a partial specification) of how to access a
  141. file. A pathname need not correspond to any file that actually exists, and more
  142. than one pathname can refer to the same file. For example, the pathname with a
  143. version of ``newest'' may refer to the same file as a pathname with the same
  144. components except a certain number as the version. Indeed, a pathname with
  145. version ``newest'' may refer to different files as time passes, because the
  146. meaning of such a pathname depends on the state of the file system. In file
  147. systems with such facilities as ``links,'' multiple file names, logical
  148. devices, and so on, two pathnames that look quite different may turn out to
  149. address the same file. To access a file given a pathname, one must do a file
  150. system operation such as open.
  151.  
  152. Two important operations involving pathnames are parsing and merging. Parsing
  153. is the conversion of a namestring (which might be something supplied
  154. interactively by the user when asked to supply the name of a file) into a
  155. pathname object. This operation is implementation-dependent, because the format
  156. of namestrings is implementation-dependent. Merging takes a pathname with
  157. missing components and supplies values for those components from a source of
  158. defaults.
  159.  
  160. Not all of the components of a pathname need to be specified. If a component of
  161. a pathname is missing, its value is nil. Before the file system interface can
  162. do anything interesting with a file, such as opening the file, all the missing
  163. components of a pathname must be filled in (typically from a set of defaults).
  164. Pathnames with missing components may be used internally for various purposes;
  165. in particular, parsing a namestring that does not specify certain components
  166. will result in a pathname with missing components.
  167.  
  168. [change_begin]
  169. X3J13 voted in January 1989 (PATHNAME-UNSPECIFIC-COMPONENT)   to permit any
  170. component of a pathname to have the value :unspecific, meaning that the
  171. component simply does not exist, for file systems in which such a value makes
  172. sense. (For example, a UNIX file system usually does not support version
  173. numbers, so the version component of a pathname for a UNIX host might be
  174. :unspecific. Similarly, the file type is usually regarded in a UNIX file system
  175. as the part of a name after a period, but some file names contain no periods
  176. and therefore have no file types.)
  177.  
  178. When a pathname is converted to a namestring, the values nil and :unspecific
  179. have the same effect: they are treated as if the component were empty (that is,
  180. they each cause the component not to appear in the namestring). When merging,
  181. however, only a nil value for a component will be replaced with the default for
  182. that component; the value :unspecific will be left alone as if the field were
  183. filled.
  184.  
  185. The results are undefined if :unspecific is supplied to a file system in a
  186. component for which :unspecific does not make sense for that file system.
  187.  
  188. Programming hint: portable programs should be prepared to handle the value
  189. :unspecific in the device, directory, type, or version field in some
  190. implementations. Portable programs should not explicitly place :unspecific in
  191. any field because it might not be permitted in some situations, but portable
  192. programs may sometimes do so implicitly (by copying such a value from another
  193. pathname, for example).
  194. [change_end]
  195.  
  196. [old_change_begin]
  197. A component of a pathname can also be the keyword :wild. This is only useful
  198. when the pathname is being used with a directory-manipulating operation, where
  199. it means that the pathname component matches anything. The printed
  200. representation of a pathname typically designates :wild by an asterisk;
  201. however, this is host-dependent.
  202. [old_change_end]
  203.  
  204. See section 23.1.4 for a discussion of new wildcard pathname facilities.
  205.  
  206. What values are allowed for components of a pathname depends, in general, on
  207. the pathname's host. However, in order for pathnames to be usable in a
  208. system-independent way, certain global conventions are adhered to. These
  209. conventions are stronger for the type and version than for the other
  210. components, since the type and version are explicitly manipulated by many
  211. programs, while the other components are usually treated as something supplied
  212. by the user that just needs to be remembered and copied from place to place.
  213.  
  214. The type is always a string or nil or :wild. It is expected that most programs
  215. that deal with files will supply a default type for each file.
  216.  
  217. The version is either a positive integer or a special symbol. The meanings of
  218. nil and :wild have been explained above. The keyword :newest refers to the
  219. largest version number that already exists in the file system when reading a
  220. file, or to a version number greater than any already existing in the file
  221. system when writing a new file. Some Common Lisp implementors may choose to
  222. define other special version symbols. Some semi-standard names, suggested but
  223. not required to be supported by every Common Lisp implementation, are :oldest,
  224. to refer to the smallest version number that exists in the file system;
  225. :previous, to refer to the version previous to the newest version; and
  226. :installed, to refer to a version that is officially installed for users (as
  227. opposed to a working or development version). Some Common Lisp implementors may
  228. also choose to attach a meaning to non-positive version numbers (a typical
  229. convention is that 0 is synonymous with :newest and -1 with :previous), but
  230. such interpretations are implementation-dependent.
  231.  
  232. The host may be a string, indicating a file system, or a list of strings, of
  233. which the first names the file system and the rest may be used for such a
  234. purpose as inter-network routing.
  235.  
  236. [old_change_begin]
  237. The device, directory, and name can each be a string (with host-dependent rules
  238. on allowed characters and length) or possibly some other Common Lisp data
  239. structure (in which case such a component is said to be structured and has an
  240. implementation-dependent format). Structured components may be used to handle
  241. such file system features as hierarchical directories. Common Lisp programs do
  242. not need to know about structured components unless they do host-dependent
  243. operations. Specifying a string as a pathname component for a host that
  244. requires a structured component will cause conversion of the string to the
  245. appropriate form.
  246. [old_change_end]
  247.  
  248. [change_begin]
  249. X3J13 voted in June 1989 (PATHNAME-SUBDIRECTORY-LIST)   to define a specific
  250. format for structured directories (see section 23.1.3).
  251.  
  252. X3J13 voted in June 1989 (PATHNAME-COMPONENT-VALUE)   to approve the following
  253. clarifications and specifications of precisely what are valid values for the
  254. various components of a pathname.
  255.  
  256. Pathname component value strings never contain the punctuation characters that
  257. are used to separate fields in a namestring (for example, slashes and periods
  258. as used in UNIX file systems). Punctuation characters appear only in
  259. namestrings. Characters used as punctuation can appear in pathname component
  260. values with a non-punctuation meaning if the file system allows it (for
  261. example, UNIX file systems allow a file name to begin with a period).
  262.  
  263. When examining pathname components, conforming programs must be prepared to
  264. encounter any of the following siutations:
  265.  
  266.    *  Any component can be nil, which means the component has not been
  267.      specified.
  268.  
  269.    *  Any component can be :unspecific, which means the component has no
  270.      meaning in this particular pathname.
  271.  
  272.    *  The device, directory, name, and type can be strings.
  273.  
  274.    *  The host can be any object, at the discretion of the implementation.
  275.  
  276.    *  The directory can be a list of strings and symbols as described in
  277.      section 23.1.3.
  278.  
  279.    *  The version can be any symbol or any integer. The symbol :newest refers
  280.      to the largest version number that already exists in the file system when
  281.      reading, overwriting, appending, superseding, or directory-listing an
  282.      existing file; it refers to the smallest version number greater than any
  283.      existing version number when creating a new file. Other symbols and
  284.      integers have implementation-defined meaning. It is suggested, but not
  285.      required, that implementations use positive integers starting at 1 as
  286.      version numbers, recognize the symbol :oldest to designate the smallest
  287.      existing version number, and use keyword symbols for other special
  288.      versions.
  289.  
  290. When examining wildcard components of a wildcard pathname, conforming programs
  291. must be prepared to encounter any of the following additional values in any
  292. component or any element of a list that is the directory component:
  293.  
  294.    *  The symbol :wild, which matches anything.
  295.  
  296.    *  A string containing implementation-dependent special wildcard characters.
  297.  
  298.    *  Any object, representing an implementation-dependent wildcard pattern.
  299.  
  300. When constructing a pathname from components, conforming programs must follow
  301. these rules:
  302.  
  303.    *  Any component may be nil. Specifying nil for the host may, in some
  304.      implementations, result in using a default host rather than an actual nil
  305.      value.
  306.  
  307.    *  The host, device, directory, name, and type may be strings. There are
  308.      implementation-dependent limits on the number and type of characters in
  309.      these strings. A plausible assumption is that letters (of a single case)
  310.      and digits are acceptable to most file systems.
  311.  
  312.  
  313.      section 23.1.3. There are implementation-dependent limits on the length
  314.      and contents of the list.
  315.  
  316.    *  The version may be :newest.
  317.  
  318.    *  Any component may be taken from the corresponding component of another
  319.      pathname. When the two pathnames are for different file systems (in
  320.      implementations that support multiple file systems), an appropriate
  321.      translation occurs. If no meaningful translation is possible, an error is
  322.      signaled. The definitions of ``appropriate'' and ``meaningful'' are
  323.      implementation-dependent.
  324.  
  325.    *  When constructing a wildcard pathname, the name, type, or version may be
  326.      :wild, which matches anything.
  327.  
  328.    *  An implementation might support other values for some components, but a
  329.      portable program should not use those values. A conforming program can use
  330.      implementation-dependent values but this can make it non-portable; for
  331.      example, it might work only with UNIX file systems.
  332.  
  333. [change_end]
  334.  
  335. The best way to compare two pathnames for equality is with equal, not eql. (On
  336. pathnames, eql is simply the same as eq.) Two pathname objects are equal if and
  337. only if all the corresponding components (host, device, and so on) are
  338. equivalent. (Whether or not uppercase and lowercase letters are considered
  339. equivalent in strings appearing in components depends on the file name
  340. conventions of the file system.) Pathnames that are equal should be
  341. functionally equivalent.
  342.  
  343. [old_change_begin]
  344. Some host file systems have features that do not fit into this pathname model.
  345. For instance, directories might be accessible as files; there might be
  346. complicated structure in the directories or names; or there might be a way to
  347. specify a directory relative to a ``current'' directory, such as the < syntax
  348. in MULTICS or the special ``..'' file name of UNIX. Such features are not
  349. allowed for by the standard Common Lisp file system interface. An
  350. implementation is free to accommodate such features in its pathname
  351. representation and provide a parser that can process such specifications in
  352. namestrings; such features are then likely to work within that single
  353. implementation. However, note that once a program depends explicitly on any
  354. such features, it will not be portable.
  355. [old_change_end]
  356.  
  357. [change_begin]
  358. X3J13 voted in June 1989 (PATHNAME-SUBDIRECTORY-LIST)   to define a specific
  359. format for structured directories (see section 23.1.3), so some of the specific
  360. examples in the previous paragraph no longer apply, but the principle is still
  361. correct.
  362. [change_end]
  363.  
  364. -------------------------------------------------------------------------------
  365.  
  366. 23.1.2. Case Conventions
  367.  
  368. [change_begin]
  369. Issues of alphabetic case in pathnames are a major source of problems. In some
  370. file systems, the customary case is lowercase, in some uppercase, in some
  371. mixed. Some file systems are case-sensitive (that is, they treat FOO and foo as
  372. different file names) and others are not.
  373.  
  374. There are two kinds of pathname case portability problems: moving programs from
  375. one Common Lisp to another, and moving pathname component values from one file
  376. system to another. The solution to the first problem is the requirement that
  377. all Common Lisp implementations that support a particular file system must use
  378. compatible representations for pathname component values. The solution to the
  379. second problem is the use of a common representation for the
  380. least-common-denominator pathname component values that exist on all
  381. interesting file systems.
  382.  
  383. Requiring a common representation directly conflicts with the desire among
  384. programmers that use only one file system to work with the local conventions
  385. and to ignore issues of porting to other file systems. The common
  386. representation cannot be the same as local (varying) conventions.
  387.  
  388. X3J13 voted in June 1989 (PATHNAME-COMPONENT-CASE)   to add a keyword argument
  389. :case to each of the functions make-pathname, pathname-host, pathname-device,
  390. pathname-directory, pathname-name, and pathname-type. The possible values for
  391. the argument are :common and :local. The default is :local.
  392.  
  393. The value :local means that strings given to make-pathname or returned by any
  394. of the pathname component accessors follow the local file system's conventions
  395. for alphabetic case. Strings given to make-pathname will be used exactly as
  396. written if the file system supports both cases. If the file system supports
  397. only one case, the strings will be translated to that case.
  398.  
  399. The value :common means that strings given to make-pathname or returned by any
  400. of the pathname component accessors follow this common convention:
  401.  
  402.    *  All uppercase means that a file system's customary case will be used.
  403.    *  All lowercase means that the opposite of the customary case will be used.
  404.    *  Mixed case represents itself.
  405.  
  406. Uppercase is used as the common case for no better reason than consistency with
  407. Lisp symbols. The second and third points allow translation from local
  408. representation to common and back to be information-preserving. (Note that
  409. translation from common to local representation and back may or may not be
  410. information-preserving, depending on the nature of the local representation.)
  411.  
  412. Namestrings always use :local file system case conventions.
  413.  
  414. Finally, merge-pathnames and translate-pathname map customary case in the input
  415. pathnames into customary case in the output pathname.
  416.  
  417. Examples of possible use of this convention:
  418.  
  419.    *  TOPS-20 is case-sensitive and prefers uppercase, translating lowercase to
  420.      uppercase unless escaped with ^V; for a TOPS-20-based file system, a
  421.      Common Lisp implementation should use identical representations for common
  422.      and local.
  423.  
  424.    *  UNIX is case-sensitive and prefers lowercase; for a UNIX-based file
  425.      system, a Common Lisp implementation should translate between common and
  426.      local representations by inverting the case of non-mixed-case strings.
  427.  
  428.    *  VAX/VMS is uppercase-only (that is, the file system translates all file
  429.      name arguments to uppercase); for a VAX/VMS-based file system, a Common
  430.      Lisp implementation should translate common representation to local by
  431.      converting to uppercase and should translate local representation to
  432.      common with no change.
  433.  
  434.    *  The Macintosh operating system is case-insensitive and prefers lowercase,
  435.      but remembers the cases of letters actually used to name a file; for a
  436.      Macintosh-based file system, a Common Lisp implementation should translate
  437.      between common and local representations by inverting the case of
  438.      non-mixed-case strings and should ignore case when determining whether two
  439.      pathnames are equal.
  440.  
  441. Here are some examples of this behavior. Assume that the host T runs TOPS-20, U
  442. runs UNIX, V runs VAX/VMS, and M runs the Macintosh operating system.
  443.  
  444. ;;; Returns two values: the PATHNAME-NAME from a namestring
  445. ;;; in :COMMON and :LOCAL representations (in that order).
  446. (defun pathname-example (name)
  447.   (let ((path (parse-namestring name))))
  448.     (values (pathname-name path :case :common)
  449.             (pathname-name path :case :local))))
  450.  
  451.                                            => "FOO" and ッ"FOO"                                               ;Common            Local
  452. (pathname-example "T:<ME>FOO.LISP")        => "FOO" and "FOO"
  453. (pathname-example "T:<ME>foo.LISP")        => "FOO" and "FOO"
  454. (pathname-example "T:<ME>^Vf^Vo^Vo.LISP")  => "foo" and "foo"
  455. (pathname-example "T:<ME>TeX.LISP")        => "TEX" and "TEX"
  456. (pathname-example "T:<ME>T^VeX.LISP")      => "TeX" and "TeX"
  457. (pathname-example "U:/me/FOO.lisp")        => "foo" and "FOO"
  458. (pathname-example "U:/me/foo.lisp")        => "FOO" and "foo"
  459. (pathname-example "U:/me/TeX.lisp")        => "TeX" and "TeX"
  460. (pathname-example "V:[me]FOO.LISP")        => "FOO" and "FOO"
  461. (pathname-example "V:[me]foo.LISP")        => "FOO" and "FOO"
  462. (pathname-example "V:[me]TeX.LISP")        => "TEX" and "TEX"
  463. (pathname-example "M:FOO.LISP")            => "foo" and "FOO"
  464. (pathname-example "M:foo.LISP")            => "FOO" and "foo"
  465. (pathname-example "M:TeX.LISP")            => "TeX" and "TeX"
  466.  
  467. The following example illustrates the creation of new pathnames. The name is
  468. converted from common representation to local because namestrings always use
  469. local conventions.
  470.  
  471. (defun make-pathname-example (h n)
  472.   (namestring (make-pathname :host h :name n :case :common))
  473.  
  474. (make-pathname-example "T" "FOO") => "T:FOO"
  475. (make-pathname-example "T" "foo") => "T:^Vf^Vo^Vo"
  476. (make-pathname-example "T" "TeX") => "T:T^VeX"
  477. (make-pathname-example "U" "FOO") => "U:foo"
  478. (make-pathname-example "U" "foo") => "U:FOO"
  479. (make-pathname-example "U" "TeX") => "U:TeX"
  480. (make-pathname-example "V" "FOO") => "V:FOO"
  481. (make-pathname-example "V" "foo") => "V:FOO"
  482. (make-pathname-example "V" "TeX") => "V:TeX"
  483. (make-pathname-example "M" "FOO") => "M:foo"
  484. (make-pathname-example "M" "foo") => "M:FOO"
  485. (make-pathname-example "M" "TeX") => "M:TeX"
  486.  
  487. A big advantage of this set of conventions is that one can, for example, call
  488. make-pathname with :type "LISP" and :case :common, and the result will appear
  489. in a namestring as .LISP or .lisp, whichever is appropriate.
  490. [change_end]
  491.  
  492. -------------------------------------------------------------------------------
  493.  
  494. 23.1.3. Structured Directories
  495.  
  496. [change_begin]
  497. X3J13 voted in June 1989 (PATHNAME-SUBDIRECTORY-LIST)   to define a specific
  498. pathname component format for structured directories.
  499.  
  500. The value of a pathname's directory component may be a list. The car of the
  501. list should be a keyword, either :absolute or :relative. Each remaining element
  502. of the list should be a string or a symbol (see below). Each string names a
  503. single level of directory structure and should consist of only the directory
  504. name without any punctuation characters.
  505.  
  506. A list whose car is the symbol :absolute represents a directory path starting
  507. from the root directory. For example, the list (:absolute) represents the root
  508. directory itself; the list (:absolute "foo" "bar" "baz") represents the
  509. directory that in a UNIX file system would be called /foo/bar/baz.
  510.  
  511. A list whose car is the symbol :relative represents a directory path starting
  512. from a default directory. The list (:relative) has the same meaning as nil and
  513. hence normally is not used. The list (:relative "foo" "bar") represents the
  514. directory named bar in the directory named foo in the default directory.
  515.  
  516. In place of a string, at any point in the list, a symbol may occur to indicate
  517. a special file notation. The following symbols have standard meanings.
  518.  
  519. :wild
  520.      Wildcard match of one level of directory structure
  521. :wild-inferiors
  522.      Wildcard match of any number of directory levels
  523. :up  Go upward in directory structure (semantic)
  524. :back
  525.      Go upward in directory structure (syntactic)
  526.  
  527. (See section 23.1.4 for a discussion of wildcard pathnames.)
  528.  
  529. Implementations are permitted to add additional objects of any non-string type
  530. if necessary to represent features of their file systems that cannot be
  531. represented with the standard strings and symbols. Supplying any non-string,
  532. including any of the symbols listed below, to a file system for which it does
  533. not make sense signals an error of type file-error. For example, most
  534. implementations of the UNIX file system do not support :wild-inferiors. Any
  535. directory list in which :absolute or :wild-inferiors is immediately followed by
  536. :up or :back is illegal and when processed causes an error to be signaled.
  537.  
  538. The keyword :back has a ``syntactic'' meaning that depends only on the pathname
  539. and not on the contents of the file system. The keyword :up has a ``semantic''
  540. meaning that depends on the contents of the file system; to resolve a pathname
  541. containing :up to a pathname whose directory component contains only :absolute
  542. and strings requires a search of the file system. Note that use of :up instead
  543. of :back can result in designating a different actual directory only in file
  544. systems that support multiple names for directories, perhaps via symbolic
  545. links. For example, suppose that there is a directory link such that
  546.  
  547. (:absolute "X" "Y")  is linked to  (:absolute "A" "B")
  548.  
  549. and there also exist directories
  550.  
  551. (:absolute "A" "Q")  and  (:absolute "X" "Q")
  552.  
  553. Then
  554.  
  555. (:absolute "X" "Y" :up "Q")  designates  (:absolute "A" "Q")
  556.  
  557. but
  558.  
  559. (:absolute "X" "Y" :back "Q")  designates  (:absolute "X" "Q")
  560.  
  561. If a string is used as the value of the :directory argument to make-pathname,
  562. it should be the name of a top-level directory and should not contain any
  563. punctuation characters. Specifying a string s is equivalent to specifying the
  564. list (:absolute s). Specifying the symbol :wild is equivalent to specifying the
  565. list (:absolute :wild-inferiors) (or (:absolute :wild) in a file system that
  566. does not support :wild-inferiors).
  567.  
  568. The function pathname-directory always returns nil, :unspecific, or a
  569. list-never a string, never :wild. If a list is returned, it is not guaranteed
  570. to be freshly consed; the consequences of modifying this list are undefined.
  571.  
  572. In non-hierarchical file systems, the only valid list values for the directory
  573. component of a pathname are (:absolute s) (where s is a string) and (:absolute
  574. :wild). The keywords :relative, :wild-inferiors, :up, and :back are not used in
  575. non-hierarchical file systems.
  576.  
  577. Pathname merging treats a relative directory specially. Let pathname and
  578. defaults be the first two arguments to merge-pathnames. If (pathname-directory
  579. pathname) is a list whose car is :relative, and (pathname-directory defaults)
  580. is a list, then the merged directory is the value of
  581.  
  582. (append (pathname-directory defaults)
  583.         (cdr     ;Remove :relative from the front
  584.           (pathname-directory pathname)))
  585.  
  586. except that if the resulting list contains a string or :wild immediately
  587. followed by :back, both of them are removed. This removal of redundant
  588. occurrences of :back is repeated as many times as possible. If
  589. (pathname-directory defaults) is not a list or (pathname-directory pathname) is
  590. not a list whose car is :relative, the merged directory is the value of
  591.  
  592. (or (pathname-directory pathname)
  593.     (pathname-directory defaults))
  594.  
  595. A relative directory in the pathname argument to a function such as open is
  596. merged with the value of *default-pathname-defaults* before the file system is
  597. accessed.
  598.  
  599. Here are some examples of the use of structured directories. Suppose that host
  600. L supports a Symbolics Lisp Machine file system, host U supports a UNIX file
  601. system, and host V supports a VAX/VMS file system.
  602.  
  603. (pathname-directory (parse-namestring "V:[FOO.BAR]BAZ.LSP"))
  604.    => (:ABSOLUTE "FOO" "BAR")
  605.  
  606. (pathname-directory (parse-namestring "U:/foo/bar/baz.lisp"))
  607.    => (:ABSOLUTE "foo" "bar")
  608.  
  609. (pathname-directory (parse-namestring "U:../baz.lisp"))
  610.    => (:RELATIVE :UP)
  611.  
  612. (pathname-directory (parse-namestring "U:/foo/bar/../mum/baz"))
  613.    => (:ABSOLUTE "foo" "bar" :UP "mum")
  614.  
  615. (pathname-directory (parse-namestring "U:bar/../../ztesch/zip"))
  616.    => (:RELATIVE "bar" :UP :UP "ztesch")
  617.  
  618. (pathname-directory (parse-namestring "L:>foo>**>bar>baz.lisp"))
  619.    => (:ABSOLUTE "FOO" :WILD-INFERIORS "BAR")
  620.  
  621. (pathname-directory (parse-namestring "L:>foo>*>bar>baz.lisp"))
  622.    => (:ABSOLUTE "FOO" :WILD "BAR")
  623.  
  624. [change_end]
  625.  
  626. -------------------------------------------------------------------------------
  627.  
  628. 23.1.4. Extended Wildcards
  629.  
  630. [change_begin]
  631. Some file systems provide more complex conventions for wildcards than simple
  632. component-wise wildcards representable by :wild. For example, the namestring
  633. "F*O" might mean a normal three-character name; a three-character name with the
  634. middle character wild; a name with at least two characters, beginning with F
  635. and ending with O; or perhaps a wild match spanning multiple directories.
  636. Similarly, the namestring ">foo>**>bar>" might imply that the middle directory
  637. is named "**"; the middle directory is :wild; there are zero or more middle
  638. directories that are :wild; or perhaps that the middle directory name matches
  639. any two-letter name. Some file systems support even more complex wildcards,
  640. such as regular expressions.
  641.  
  642. X3J13 voted in June 1989 (PATHNAME-WILD)   to provide some facilities for
  643. dealing with more general wildcard pathnames in a fairly portable manner.
  644.  
  645. [Function]
  646. wild-pathname-p pathname &optional field-key
  647.  
  648. Tests a pathname for the presence of wildcard components. If the first argument
  649. is not a pathname, string, or file stream, an error of type type-error is
  650. signaled.
  651.  
  652. If no field-key is provided, or the field-key is nil, the result is true if and
  653. only if pathname has any wildcard components.
  654.  
  655. If a non-null field-key is provided, it must be one of :host, :device,
  656. :directory, :name, :type, or :version. In this case, the result is true if and
  657. only if the indicated component of pathname is a wildcard.
  658.  
  659. Note that X3J13 voted in June 1989 (PATHNAME-COMPONENT-VALUE)   to specify that
  660. an implementation need not support wildcards in all fields; the only
  661. requirement is that the name, type, or version may be :wild. However, portable
  662. programs should be prepared to encounter either :wild or
  663. implementation-dependent wildcards in any pathname component. The function
  664. wild-pathname-p provides a portable way for testing the presence of wildcards.
  665.  
  666. [Function]
  667. pathname-match-p pathname wildname
  668.  
  669. This predicate is true if and only if the pathname matches the wildname. The
  670. matching rules are implementation-defined but should be consistent with the
  671. behavior of the directory function. Missing components of wildname default to
  672. :wild.
  673.  
  674. If either argument is not a pathname, string, or file stream, an error of type
  675. type-error is signaled. It is valid for pathname to be a wild pathname; a
  676. wildcard field in pathname will match only a wildcard field in wildname; that
  677. is, pathname-match-p is not commutative. It is valid for wildname to be a
  678. non-wild pathname; I believe that in this case pathname-match-p will have the
  679. same behavior as equal, though the X3J13 specification did not say so.
  680.  
  681. [Function]
  682. translate-pathname source from-wildname to-wildname &key
  683.  
  684. Translates the pathname source, which must match from-wildname, into a
  685. corresponding pathname (call it result), which is constructed so as to match
  686. to-wildname, and returns result.
  687.  
  688. The pathname result is a copy of to-wildname with each missing or wildcard
  689. field replaced by a portion of source; for this purpose a wildcard field is a
  690. pathname component with a value of :wild, a :wild element of a list-valued
  691. directory component, or an implementation-defined portion of a component, such
  692. as the * in the complex wildcard string "foo*bar" that some implementations
  693. support. An implementation that adds other wildcard features, such as regular
  694. expressions, must define how translate-pathname extends to those features. A
  695. missing field is a pathname component that is nil.
  696.  
  697. The portion of source that is copied into result is implementation-defined.
  698. Typically it is determined by the user interface conventions of the file
  699. systems involved. Usually it is the portion of source that matches a wildcard
  700. field of from-wildname that is in the same position as the missing or wildcard
  701. field of to-wildname. If there is no wildcard field in from-wildname at that
  702. position, then usually it is the entire corresponding pathname component of
  703. source or, in the case of a list-valued directory component, the entire
  704. corresponding list element. For example, if the name components of source,
  705. from-wildname, and to-wildname are "gazonk", "gaz*", and "h*" respectively,
  706. then in most file systems the wildcard fields of the name component of
  707. from-wildname and to-wildname are each "*", the matching portion of source is
  708. "onk", and the name component of result is "honk"; however, the exact behavior
  709. of translate-pathname is not dictated by the Common Lisp language and may vary
  710. according to the user interface conventions of the file systems involved.
  711.  
  712. During the copying of a portion of source into result, additional
  713. implementation-defined translations of alphabetic case or file naming
  714. conventions may occur, especially when from-wildname and to-wildname are for
  715. different hosts.
  716.  
  717. If any of the first three arguments is not a pathname, string, or file stream,
  718. an error of type type-error is signaled. It is valid for source to be a wild
  719. pathname; in general this will produce a wild result pathname. It is valid for
  720. from-wildname or to-wildname or both to be non-wild. An error is signaled if
  721. the source pathname does not match the from-wildname, that is, if
  722. (pathname-match-p source from-wildname) would not be true.
  723.  
  724. There are no specified keyword arguments for translate-pathname, but
  725. implementations are permitted to extend it by adding keyword arguments. There
  726. is one specified return value from translate-pathname; implementations are
  727. permitted to extend it by returning additional values.
  728.  
  729. Here is an implementation suggestion. One file system performs this operation
  730. by examining corresponding pieces of the three pathnames in turn, where a piece
  731. is a pathname component or a list element of a structured component such as a
  732. hierarchical directory. Hierarchical directory elements in from-wildname and
  733. to-wildname are matched by whether they are wildcards, not by depth in the
  734. directory hierarchy. If the piece in to-wildname is present and not wild, it is
  735. copied into the result. If the piece in to-wildname is :wild or nil, the
  736. corresponding piece in source is copied into the result. Otherwise, the piece
  737. in to-wildname might be a complex wildcard such as "foo*bar"; the portion of
  738. the piece in source that matches the wildcard portion of the corresponding
  739. piece in from-wildname (or the entire source piece, if the from-wildname piece
  740. is not wild and therefore equals the source piece) replaces the wildcard
  741. portion of the piece in to-wildname and the value produced is used in the
  742. result.
  743.  
  744. X3J13 voted in June 1989 (PATHNAME-COMPONENT-CASE)   to require
  745. translate-pathname to map customary case in argument pathnames to the customary
  746. case in returned pathnames (see section 23.1.2).
  747.  
  748. Here are some examples of the use of the new wildcard pathname facilities.
  749. These examples are not portable. They are written to run with particular file
  750. systems and particular wildcard conventions and are intended to be
  751. illustrative, not prescriptive. Other implementations may behave differently.
  752.  
  753. (wild-pathname-p (make-pathname :name :wild)) => t
  754. (wild-pathname-p (make-pathname :name :wild) :name) => t
  755. (wild-pathname-p (make-pathname :name :wild) :type) => nil
  756. (wild-pathname-p (pathname "S:>foo>**>")) => t         ;Maybe
  757. (wild-pathname-p (make-pathname :name "F*O")) => t     ;Probably
  758.  
  759. One cannot rely on rename-file to handle wild pathnames in a predictable
  760. manner. However, one can use translate-pathname explicitly to control the
  761. process.
  762.  
  763. (defun rename-files (from to)
  764.   "Rename all files that match the first argument by
  765.    translating their names to the form of the second
  766.    argument.  Both arguments may be wild pathnames."
  767.   (dolist (file (directory from))
  768.     ;; DIRECTORY produces only pathnames that match from-wildname.
  769.     (rename-file file (translate-pathname file from to))))
  770.  
  771. Assuming one particular set of popular wildcard conventions, this function
  772. might exhibit the following behavior. Not all file systems will run this
  773. example exactly as written.
  774.  
  775. (rename-files "/usr/me/*.lisp" "/dev/her/*.l")
  776.    renames  /usr/me/init.lisp
  777.         to  /dev/her/init.l
  778.  
  779. (rename-files "/usr/me/pcl*/*" "/sys/pcl/*/")
  780.    renames  /usr/me/pcl-5-may/low.lisp
  781.         to  /sys/pcl/pcl-5-may/low.lisp
  782.    (in some file systems the result might be /sys/pcl/5-may/low.lisp)
  783.  
  784. (rename-files "/usr/me/pcl*/*" "/sys/library/*/")
  785.    renames  /usr/me/pcl-5-may/low.lisp
  786.         to  /sys/library/pcl-5-may/low.lisp
  787.    (in some file systems the result might be /sys/library/5-may/low.lisp)
  788.  
  789. (rename-files "/usr/me/foo.bar" "/usr/me2/")
  790.    renames  /usr/me/foo.bar
  791.         to  /usr/me2/foo.bar
  792.  
  793. (rename-files "/usr/joe/*-recipes.text"
  794.               "/usr/jim/personal/cookbook/joe's-*-rec.text")
  795.    renames  /usr/joe/lamb-recipes.text
  796.         to  /usr/jim/personal/cookbook/joe's-lamb-rec.text
  797.    renames  /usr/joe/veg-recipes.text
  798.         to  /usr/jim/personal/cookbook/joe's-veg-rec.text
  799.    renames  /usr/joe/cajun-recipes.text
  800.         to  /usr/jim/personal/cookbook/joe's-cajun-rec.text
  801.    renames  /usr/joe/szechuan-recipes.text
  802.         to  /usr/jim/personal/cookbook/joe's-szechuan-rec.text
  803.  
  804. The following examples use UNIX syntax and the wildcard conventions of one
  805. particular version of UNIX.
  806.  
  807. (namestring
  808.   (translate-pathname "/usr/dmr/hacks/frob.l"
  809.                       "/usr/d*/hacks/*.l"
  810.                       "/usr/d*/backup/hacks/backup-*.*"))
  811.    => "/usr/dmr/backup/hacks/backup-frob.l"
  812.  
  813. (namestring
  814.   (translate-pathname "/usr/dmr/hacks/frob.l"
  815.                       "/usr/d*/hacks/fr*.l"
  816.                       "/usr/d*/backup/hacks/backup-*.*"))
  817.    => "/usr/dmr/backup/hacks/backup-ob.l"
  818.  
  819. The following examples are similar to the preceding examples but use two
  820. different hosts; host U supports a UNIX file system and host V supports a
  821. VAX/VMS file system. Note the translation of file type (from l to LSP) and the
  822. change of alphabetic case conventions.
  823.  
  824. (namestring
  825.   (translate-pathname "U:/usr/dmr/hacks/frob.l"
  826.                       "U:/usr/d*/hacks/*.l"
  827.                       "V:SYS$DISK:[D*.BACKUP.HACKS]BACKUP-*.*"))
  828.    => "V:SYS$DISK:[DMR.BACKUP.HACKS]BACKUP-FROB.LSP"
  829.  
  830. (namestring
  831.   (translate-pathname "U:/usr/dmr/hacks/frob.l"
  832.                       "U:/usr/d*/hacks/fr*.l"
  833.                       "V:SYS$DISK:[D*.BACKUP.HACKS]BACKUP-*.*"))
  834.    => "V:SYS$DISK:[DMR.BACKUP.HACKS]BACKUP-OB.LSP"
  835.  
  836. The next example is a version of the function translate-logical-pathname
  837. (simplified a bit) for a logical host named FOO. The points of interest are the
  838. use of pathname-match-p as a :test argument for assoc and the use of
  839. translate-pathname as a substrate for translate-logical-pathname.
  840.  
  841. (define-condition logical-translation-error (file-error))
  842.  
  843. (defun my-translate-logical-pathname (pathname &key rules)
  844.   (let ((rule (assoc pathname rules :test #'pathname-match-p)))
  845.     (unless rule
  846.       (error 'logical-translation-error :pathname pathname))
  847.     (translate-pathname pathname (first rule) (second rule))))
  848.  
  849. (my-translate-logical-pathname
  850.   "FOO:CODE;BASIC.LISP"
  851.   :rules '(("FOO:DOCUMENTATION;" "U:/doc/foo/")
  852.            ("FOO:CODE;"          "U:/lib/foo/")
  853.            ("FOO:PATCHES;*;"     "U:/lib/foo/patch/*/")))
  854.    => #P"U:/lib/foo/basic.l"
  855.  
  856. [change_end]
  857.  
  858. -------------------------------------------------------------------------------
  859.  
  860. 23.1.5. Logical Pathnames
  861.  
  862. [change_begin]
  863. Pathname values are not portable, but sometimes they must be mentioned in a
  864. program (for example, the names of files containing the program and the data
  865. used by the program).
  866.  
  867. X3J13 voted in June 1989 (PATHNAME-LOGICAL)   to provide some facilities for
  868. portable pathname values. The idea is to provide a portable framework for
  869. pathname values; these logical pathnames are then mapped to physical (that is,
  870. actual) pathnames by a set of implementation-dependent or site-dependent rules.
  871. The logical pathname facility therefore separates the concerns of program
  872. writing and user software architecture from the details of how a software
  873. system is embedded in a particular file system or operating environment.
  874.  
  875. Pathname values are not portable because not all Common Lisp implementations
  876. use the same operating system and file name syntax varies widely among
  877. operating systems. In addition, corresponding files at two different sites may
  878. have different names even when the operating system is the same; for example,
  879. they may be on different directories or different devices. The Common Lisp
  880. logical pathname system defines a particular pathname structure and namestring
  881. syntax that must be supported by all implementations.
  882.  
  883. [Class]
  884. logical-pathname
  885.  
  886. This is a subclass of pathname.
  887. [change_end]
  888.  
  889. -------------------------------------------------------------------------------
  890.  
  891.    *  Syntax of Logical Pathname Namestrings
  892.    *  Parsing of Logical Pathname Namestrings
  893.    *  Using Logical Pathnames
  894.    *  Examples of the Use of Logical Pathnames
  895.    *  Discussion of Logical Pathnames
  896.  
  897. -------------------------------------------------------------------------------
  898.  
  899. 23.1.5.1. Syntax of Logical Pathname Namestrings
  900.  
  901. [change_begin]
  902. The syntax of a logical pathname namestring is as follows:
  903.  
  904. logical-namestring ::= [host :] [;] {directory ;}*
  905.       [name] [. type [. version]
  906.  
  907. Note that a logical namestring has no device portion.
  908.  
  909. host ::= word
  910. directory ::= word | wildcard-word | wildcard-inferiors
  911. name ::= word | wildcard-word
  912. type ::= word | wildcard-word
  913. version ::= word | wildcard-word
  914. word ::= {letter digit -}+
  915. wildcard-word ::= [word] * {word *}* [word]
  916. wildcard-inferiors ::= **
  917.  
  918. A word consists of one or more uppercase letters, digits, and hyphens.
  919.  
  920. A wildcard word consists of one or more asterisks, uppercase letters, digits,
  921. and hyphens, including at least one asterisk, with no two asterisks adjacent.
  922. Each asterisk matches a sequence of zero or more characters. The wildcard word
  923. * parses as :wild; all others parse as strings.
  924.  
  925. Lowercase letters may also appear in a word or wildcard word occurring in a
  926. namestring. Such letters are converted to uppercase when the namestring is
  927. converted to a pathname. The consequences of using other characters are
  928. unspecified.
  929.  
  930. The host is a word that has been defined as a logical pathname host by using
  931. setf with the function logical-pathname-translations.
  932.  
  933. There is no device, so the device component of a logical pathname is always
  934. :unspecific. No other component of a logical pathname can be :unspecific.
  935.  
  936. Each directory is a word, a wildcard word, or ** (which is parsed as
  937. :wild-inferiors). If a semicolon precedes the directories, the directory
  938. component is relative; otherwise it is absolute.
  939.  
  940. The name is a word or a wildcard word.
  941.  
  942. The type is a word or a wildcard word.
  943.  
  944. The version is a positive decimal integer or the word NEWEST (which is parsed
  945. as :newest) or * (which is parsed as :wild). The letters in NEWEST can be in
  946. either alphabetic case.
  947.  
  948. The consequences of using any value not specified here as a logical pathname
  949. component are unspecified. The null string "" is not a valid value for any
  950. component of a logical pathname, since the null string is not a word or a
  951. wildcard word.
  952. [change_end]
  953.  
  954. -------------------------------------------------------------------------------
  955.  
  956. 23.1.5.2. Parsing of Logical Pathname Namestrings
  957.  
  958. [change_begin]
  959. Logical pathname namestrings are recognized by the functions logical-pathname
  960. and translate-logical-pathname. The host portion of the logical pathname
  961. namestring and its following colon must appear in the namestring arguments to
  962. these functions.
  963.  
  964. The function parse-namestring recognizes a logical pathname namestring when the
  965. host argument is logical or the defaults argument is a logical pathname. In
  966. this case the host portion of the logical pathname namestring and its following
  967. colon are optional. If the host portion of the namestring and the host argument
  968. are both present and do not match, an error is signaled. The host argument is
  969. logical if it is supplied and came from pathname-host of a logical pathname.
  970. Whether a host argument is logical if it is a string equal to a logical
  971. pathname host name is implementation-defined.
  972.  
  973. The function merge-pathnames recognizes a logical pathname namestring when the
  974. defaults argument is a logical pathname. In this case the host portion of the
  975. logical pathname namestring and its following colon are optional.
  976.  
  977. Whether the other functions that coerce strings to pathnames recognize logical
  978. pathname namestrings is implementation-defined. These functions include
  979. parse-namestring in circumstances other than those described above,
  980. merge-pathnames in circumstances other than those described above, the
  981. :defaults argument to make-pathname, and the following functions:
  982.  
  983. compile-file           file-write-date     pathname-name
  984. compile-file-pathname  host-namestring     pathname-type
  985. delete-file            load                pathname-version
  986. directory              namestring          probe-file
  987. directory-namestring   open                rename-file
  988. dribble                pathname            translate-pathname
  989. ed                     pathname-device     truename
  990. enough-namestring      pathname-directory  wild-pathname-p
  991. file-author            pathname-host       with-open-file
  992. file-namestring        pathname-match-p
  993.  
  994. Note that many of these functions must accept logical pathnames even though
  995. they do not accept logical pathname namestrings.
  996. [change_end]
  997.  
  998. -------------------------------------------------------------------------------
  999.  
  1000. 23.1.5.3. Using Logical Pathnames
  1001.  
  1002. [change_begin]
  1003. Some real file systems do not have versions. Logical pathname translation to
  1004. such a file system ignores the version. This implies that a portable program
  1005. cannot rely on being able to store in a file system more than one version of a
  1006. file named by a logical pathname.
  1007.  
  1008. The type of a logical pathname for a Common Lisp source file is LISP. This
  1009. should be translated into whatever implementation-defined type is appropriate
  1010. in a physical pathname.
  1011.  
  1012. The logical pathname host name SYS is reserved for the implementation. The
  1013. existence and meaning of logical pathnames for logical host SYS is
  1014. implementation-defined.
  1015.  
  1016. File manipulation functions must operate with logical pathnames according to
  1017. the following requirements:
  1018.  
  1019.    *  The following accept logical pathnames and translate them into physical
  1020.      pathnames as if by calling the function translate-logical-pathname:
  1021.  
  1022.      compile-file           ed               probe-file
  1023.      compile-file-pathname  file-author      rename-file
  1024.      delete-file            file-write-date  truename
  1025.      directory              load             with-open-file
  1026.      dribble                open
  1027.  
  1028.    *  Applying the function pathname to a stream created by the function open
  1029.      or the macro with-open-file using a logical pathname produces a logical
  1030.      pathname.
  1031.  
  1032.    *  The functions truename, probe-file, and directory never return logical
  1033.      pathnames.
  1034.  
  1035.    *  Calling rename-file with a logical pathname as the second argument
  1036.      returns a logical pathname as the first value.
  1037.  
  1038.    *  make-pathname returns a logical pathname if and only if the host is
  1039.      logical. If the :host argument to make-pathname is supplied, the host is
  1040.      logical if it came from the pathname-host of a logical pathname. Whether a
  1041.      :host argument is logical if it is a string equal to a logical pathname
  1042.      host name is implementation-defined.
  1043.  
  1044. [Function]
  1045. logical-pathname pathname
  1046.  
  1047. Converts the argument to a logical pathname and returns it. The argument can be
  1048. a logical pathname, a logical pathname namestring containing a host component,
  1049. or a stream for which the pathname function returns a logical pathname. For any
  1050. other argument, logical-pathname signals an error of type type-error.
  1051.  
  1052. [Function]
  1053. translate-logical-pathname pathname &key
  1054.  
  1055. Translates a logical pathname to the corresponding physical pathname. The
  1056. pathname argument is first coerced to a pathname. If it is not a pathname,
  1057. string, or file stream, an error of type type-error is signaled.
  1058.  
  1059. If the coerced argument is a physical pathname, it is returned.
  1060.  
  1061. If the coerced argument is a logical pathname, the first matching translation
  1062. (according to pathname-match-p) of the logical pathname host is applied, as if
  1063. by calling translate-pathname. If the result is a logical pathname, this
  1064. process is repeated. When the result is finally a physical pathname, it is
  1065. returned.
  1066.  
  1067. If no translation matches a logical pathname, an error of type file-error is
  1068. signaled.
  1069.  
  1070. translate-logical-pathname may perform additional translations, typically to
  1071. provide translation of file types to local naming conventions, to accommodate
  1072. physical file systems with names of limited length, or to deal with special
  1073. character requirements such as translating hyphens to underscores or uppercase
  1074. letters to lowercase. Any such additional translations are
  1075. implementation-defined. Some implementations do no additional translations.
  1076.  
  1077. There are no specified keyword arguments for translate-logical-pathname but
  1078. implementations are permitted to extend it by adding keyword arguments. There
  1079. is one specified return value from translate-logical-pathname; implementations
  1080. are permitted to extend it by returning additional values.
  1081.  
  1082. [Function]
  1083. logical-pathname-translations host
  1084.  
  1085. If the specified host is not the host component of a logical pathname and is
  1086. not a string that has been defined as a logical pathname host name by setf of
  1087. logical-pathname-translations, this function signals an error of type
  1088. type-error; otherwise, it returns the list of translations for the specified
  1089. host. Each translation is a list of at least two elements, from-wildname and
  1090. to-wildname. Any additional elements are implementation-defined. A
  1091. from-wildname is a logical pathname whose host is the specified host. A
  1092. to-wildname is any pathname. Translations are searched in the order listed, so
  1093. more specific from-wildnames must precede more general ones.
  1094.  
  1095. (setf (logical-pathname-translations host) translations) sets the list of
  1096. translations for the logical pathname host to translations. If host is a string
  1097. that has not previously been used as logical pathname host, a new logical
  1098. pathname host is defined; otherwise an existing host's translations are
  1099. replaced. Logical pathname host names are compared with string-equal.
  1100.  
  1101. When setting the translations list, each from-wildname can be a logical
  1102. pathname whose host is host or a logical pathname namestring s parseable by
  1103. (parse-namestring s host-object), where host-object is an appropriate object
  1104. for representing the specified host to parse-namestring. (This circuitous
  1105. specification dodges the fact that parse-namestring does not necessarily accept
  1106. as its second argument any old string that names a logical host.) Each
  1107. to-wildname can be anything coercible to a pathname by application of the
  1108. function pathname. If to-wildname coerces to a logical pathname,
  1109. translate-logical-pathname will retranslate the result, repeatedly if
  1110. necessary.
  1111.  
  1112. Implementations may define additional functions that operate on logical
  1113. pathname hosts (for example, to specify additional translation rules or
  1114. options).
  1115.  
  1116. [Function]
  1117. load-logical-pathname-translations host
  1118.  
  1119. If a logical pathname host named host (a string) is already defined, this
  1120. function returns nil. Otherwise, it searches for a logical pathname host
  1121. definition in an implementation-defined manner. If none is found, it signals an
  1122. error. If a definition is found, it installs the definition and returns t.
  1123.  
  1124. The search used by load-logical-pathname-translations should be documented, as
  1125. logical pathname definitions will be created by users as well as by Lisp
  1126. implementors. A typical search technique is to look in an
  1127. implementation-defined directory for a file whose name is derived from the host
  1128. name in an implementation-defined fashion.
  1129.  
  1130. [Function]
  1131. compile-file-pathname pathname &key :output-file
  1132.  
  1133. Returns the pathname that compile-file would write into, if given the same
  1134. arguments. If the pathname argument is a logical pathname and the :output-file
  1135. argument is unspecified, the result is a logical pathname. If an implementation
  1136. supports additional keyword arguments to compile-file, compile-file-pathname
  1137. must accept the same arguments.
  1138. [change_end]
  1139.  
  1140. -------------------------------------------------------------------------------
  1141.  
  1142. 23.1.5.4. Examples of the Use of Logical Pathnames
  1143.  
  1144. [change_begin]
  1145. Here is a very simple example of setting up a logical pathname host named FOO.
  1146. Suppose that no translations are necessary to get around file system
  1147. restrictions, so all that is necessary is to specify the root of the physical
  1148. directory tree that contains the logical file system. The namestring syntax in
  1149. the to-wildname is implementation-specific.
  1150.  
  1151. (setf (logical-pathname-translations "foo")
  1152.       '(("**;*.*.*"          "MY-LISPM:>library>foo>**>")))
  1153.  
  1154. The following is a sample use of that logical pathname. All return values are
  1155. of course implementation-specific; all of the examples in this section are of
  1156. course meant to be illustrative and not prescriptive.
  1157.  
  1158. (translate-logical-pathname "foo:bar;baz;mum.quux.3")
  1159.    => #P"MY-LISPM:>library>foo>bar>baz>mum.quux.3"
  1160.  
  1161. Next we have a more complex example, dividing the files among two file servers
  1162. (U, supporting a UNIX file system, and V, supporting a VAX/VMS file system) and
  1163. several different directories. This UNIX file system doesn't support
  1164. :wild-inferiors in the directory, so each directory level must be translated
  1165. individually. No file name or type translations are required except for .MAIL
  1166. to .MBX. The namestring syntax used for the to-wildnames is
  1167. implementation-specific.
  1168.  
  1169. (setf (logical-pathname-translations "prog")
  1170.       '(("RELEASED;*.*.*"    "U:/sys/bin/my-prog/")
  1171.         ("RELEASED;*;*.*.*"  "U:/sys/bin/my-prog/*/")
  1172.         ("EXPERIMENTAL;*.*.*"
  1173.                              "U:/usr/Joe/development/prog/")
  1174.         ("EXPERIMENTAL;DOCUMENTATION;*.*.*"
  1175.                              "V:SYS$DISK:[JOE.DOC]")
  1176.         ("EXPERIMENTAL;*;*.*.*"
  1177.                              "U:/usr/Joe/development/prog/*/")
  1178.         ("MAIL;**;*.MAIL"    "V:SYS$DISK:[JOE.MAIL.PROG...]*.MBX")
  1179.         ))
  1180.  
  1181. Here are sample uses of logical host PROG. All return values are of course
  1182. implementation-specific.
  1183.  
  1184. (translate-logical-pathname "prog:mail;save;ideas.mail.3")
  1185.    => #P"V:SYS$DISK:[JOE.MAIL.PROG.SAVE]IDEAS.MBX.3"
  1186.  
  1187. (translate-logical-pathname "prog:experimental;spreadsheet.c")
  1188.    => #P"U:/usr/Joe/development/prog/spreadsheet.c"
  1189.  
  1190. Suppose now that we have a program that uses three files logically named
  1191. MAIN.LISP, AUXILIARY.LISP, and DOCUMENTATION.LISP. The following translations
  1192. might be provided by a software supplier as examples.
  1193.  
  1194. For a UNIX file system with long file names:
  1195.  
  1196. (setf (logical-pathname-translations "prog")
  1197.       '(("CODE;*.*.*"        "/lib/prog/")))
  1198.  
  1199. (translate-logical-pathname "prog:code;documentation.lisp")
  1200.    => #P"/lib/prog/documentation.lisp"
  1201.  
  1202. For a UNIX file system with 14-character file names, using .lisp as the type:
  1203.  
  1204. (setf (logical-pathname-translations "prog")
  1205.       '(("CODE;DOCUMENTATION.*.*" "/lib/prog/docum.*")
  1206.         ("CODE;*.*.*"             "/lib/prog/")))
  1207.  
  1208. (translate-logical-pathname "prog:code;documentation.lisp")
  1209.    => #P"/lib/prog/docum.lisp"
  1210.  
  1211. For a UNIX file system with 14-character file names, using .l as the type (the
  1212. second translation shortens the compiled file type to .b):
  1213.  
  1214. (setf (logical-pathname-translations "prog")
  1215.       `(("**;*.LISP.*"      ,(logical-pathname "PROG:**;*.L.*"))
  1216.         (,(compile-file-pathname
  1217.             (logical-pathname "PROG:**;*.LISP.*"))
  1218.                             ,(logical-pathname "PROG:**;*.B.*"))
  1219.         ("CODE;DOCUMENTATION.*.*" "/lib/prog/documentatio.*")
  1220.         ("CODE;*.*.*"             "/lib/prog/")))
  1221.  
  1222. (translate-logical-pathname "prog:code;documentation.lisp")
  1223.    => #P"/lib/prog/documentatio.l"
  1224.  
  1225. [change_end]
  1226.  
  1227. -------------------------------------------------------------------------------
  1228.  
  1229. 23.1.5.5. Discussion of Logical Pathnames
  1230.  
  1231. [change_begin]
  1232.  
  1233. Large programs can be moved between sites without changing any pathnames,
  1234. provided all pathnames used are logical. A portable system construction tool
  1235. can be created that operates on programs defined as sets of files named by
  1236. logical pathnames.
  1237.  
  1238. Logical pathname syntax was chosen to be easily translated into the formats of
  1239. most popular file systems, while still being powerful enough for storing large
  1240. programs. Although they have hierarchical directories, extended wildcard
  1241. matching, versions, and no limit on the length of names, logical pathnames can
  1242. be mapped onto a less capable real file system by translating each directory
  1243. that is used into a flat directory name, processing wildcards in the Lisp
  1244. implementation rather than in the file system, treating all versions as
  1245. :newest, and using translations to shorten long names.
  1246.  
  1247. Logical pathname words are restricted to non-case-sensitive letters, digits,
  1248. and hyphens to avoid creating problems with real file systems that support
  1249. limited character sets for file naming. (If logical pathnames were
  1250. case-sensitive, it would be very difficult to map them into a file system that
  1251. is not sensitive to case in its file names.)
  1252.  
  1253. It is not a goal of logical pathnames to be able to represent all possible file
  1254. names. Their goal is rather to represent just enough file names to be useful
  1255. for storing software. Real pathnames, in contrast, need to provide a uniform
  1256. interface to all possible file names, including names and naming conventions
  1257. that are not under the control of Common Lisp.
  1258.  
  1259. The choice of logical pathname syntax, using colon, semicolon, and period, was
  1260. guided by the goals of being visually distinct from real file systems and
  1261. minimizing the use of special characters.
  1262.  
  1263. The logical-pathname function is separate from the pathname function so that
  1264. the syntax of logical pathname namestrings does not constrain the syntax of
  1265. physical pathname namestrings in any way. Logical pathname syntax must be
  1266. defined by Common Lisp so that logical pathnames can be conveniently exchanged
  1267. between implementations, but physical pathname syntax is dictated by the
  1268. operating environments.
  1269.  
  1270. The compile-file-pathname function and the specification of LISP as the type of
  1271. a logical pathname for a Common Lisp source file together provide enough
  1272. information about compilation to make possible a portable system construction
  1273. tool. Suppose that it is desirable to call compile-file only if the source file
  1274. is newer than the compiled file. For this to succeed, it must be possible to
  1275. know the name of the compiled file without actually calling compile-file. In
  1276. some implementations the compiler produces one of several file types, depending
  1277. on a variety of implementation-dependent circumstances, so it is not sufficient
  1278. simply to prescribe a standard logical file type for compiled files;
  1279. compile-file-pathname provides access to the defaulting that is performed by
  1280. compile-file ``in a manner appropriate to the implementation's file system
  1281. conventions.''
  1282.  
  1283. The use of the logical pathname host name SYS for the implementation is current
  1284. practice. Standardizing on this name helps users choose logical pathname host
  1285. names that avoid conflicting with implementation-defined names.
  1286.  
  1287. Loading of logical pathname translations from a site-dependent file allows
  1288. software to be distributed using logical pathnames. The assumed model of
  1289. software distribution is a division of labor between the supplier of the
  1290. software and the user installing it. The supplier chooses logical pathnames to
  1291. name all the files used or created by the software, and supplies examples of
  1292. logical pathname translations for a few popular file systems. Each example uses
  1293. an assumed directory and/or device name, assumes local file naming conventions,
  1294. and provides translations that will translate all the logical pathnames used or
  1295. generated by the particular software into valid physical pathnames. For a
  1296. powerful file system these translations can be quite simple. For a more
  1297. restricted file system, it may be necessary to list an explicit translation for
  1298. every logical pathname used (for example, when dealing with restrictions on the
  1299. maximum length of a file name).
  1300.  
  1301. The user installing the software decides on which device and directory to store
  1302. the files and edits the example logical pathname translations accordingly. If
  1303. necessary, the user also adjusts the translations for local file naming
  1304. conventions and any other special aspects of the user's local file system
  1305. policy and local Common Lisp implementation. For example, the files might be
  1306. divided among several file server hosts to share the load. The process of
  1307. defining site-customized logical pathname translations is quite easy for a user
  1308. of a popular file system for which the software supplier has provided an
  1309. example. A user of a more unusual file system might have to take more time; the
  1310. supplier can help by providing a list of all the logical pathnames used or
  1311. generated by the software.Once the user has created and executed a suitable
  1312. setf form for setting the logical-pathname-translations of the relevant logical
  1313. host, the software can be loaded and run. It may be necessary to use the
  1314. translations again, or on another workstation at the same site, so it is best
  1315. to save the setf form in the standard place where it can be found later by
  1316. load-logical-pathname-translations. Often a software supplier will include a
  1317. program for restoring software from the distribution medium to the file system
  1318. and a program for loading the software from the file system into a Common Lisp;
  1319. these programs will start by calling load-logical-pathname-translations to make
  1320. sure that the logical pathname host is defined.
  1321.  
  1322. Note that the setf of logical-pathname-translations form isn't part of the
  1323. program; it is separate and is written by the user, not by the software
  1324. supplier. That separation and a uniform convention for doing the separation are
  1325. the key aspects of logical pathnames. For small programs involving only a
  1326. handful of files, it doesn't matter much. The real benefits come with large
  1327. programs with hundreds or thousands of files and more complicated situations
  1328. such as program-generated file names or porting a program developed on a system
  1329. with long file names onto a system with a very restrictive limit on the length
  1330. of file names.
  1331. [change_end]
  1332.  
  1333. -------------------------------------------------------------------------------
  1334.  
  1335. 23.1.6. Pathname Functions
  1336.  
  1337. These functions are what programs use to parse and default file names that have
  1338. been typed in or otherwise supplied by the user.
  1339.  
  1340. [old_change_begin]
  1341. Any argument called pathname in this book may actually be a pathname, a string
  1342. or symbol, or a stream. Any argument called defaults may likewise be a
  1343. pathname, a string or symbol, or a stream.
  1344. [old_change_end]
  1345.  
  1346. [change_begin]
  1347. X3J13 voted in March 1988 (PATHNAME-SYMBOL)   to change the language so that a
  1348. symbol is never allowed as a pathname argument. More specifically, the
  1349. following functions are changed to disallow a symbol as a pathname argument:
  1350.  
  1351. pathname          pathname-device     namestring
  1352. truename          pathname-directory  file-namestring
  1353. parse-namestring  pathname-name       directory-namestring
  1354. merge-pathnames   pathname-type       host-namestring
  1355. pathname-host     pathname-version    enough-namestring
  1356.  
  1357. (The function require was also changed by this vote but was deleted from the
  1358. language by a vote in January 1989 (REQUIRE-PATHNAME-DEFAULTS)   .)
  1359. Furthermore, the vote reaffirmed that the following functions do not accept
  1360. symbols as file, filename, or pathname arguments:
  1361.  
  1362. open             rename-file     file-write-date
  1363. with-open-file   delete-file     file-author
  1364. load             probe-file      directory
  1365. compile-file
  1366.  
  1367. In older implementations of Lisp that did not have strings, for example
  1368. MacLisp, symbols were the only means for specifying pathnames. This was
  1369. convenient only because the file systems of the time allowed only uppercase
  1370. letters in file names. Typing (load 'foo) caused the function load to receive
  1371. the symbol FOO (with uppercase letters because of the way symbols are parsed)
  1372. and therefore to load the file named FOO. Now that many file systems, most
  1373. notably UNIX, support case-sensitive file names, the use of symbols is less
  1374. convenient and more error-prone.
  1375.  
  1376. X3J13 voted in March 1988 (PATHNAME-STREAM)   to specify that a stream may be
  1377. used as a pathname, file, or filename argument only if it was created by use of
  1378. open or with-open-file, or if it is a synonym stream whose symbol is bound to a
  1379. stream that may be used as a pathname.
  1380.  
  1381. If such a stream is used as a pathname, it is as if the pathname function were
  1382. applied to the stream and the resulting pathname used in place of the stream.
  1383. This represents the name used to open the file. This may be, but is not
  1384. required to be, the actual name of the file.
  1385.  
  1386. It is an error to attempt to obtain a pathname from a stream created by any of
  1387. the following:
  1388.  
  1389. make-two-way-stream              make-string-input-stream
  1390. make-echo-stream                 make-string-output-stream
  1391. make-broadcast-stream            with-input-from-string
  1392. make-concatenated-stream         with-output-to-string
  1393.  
  1394. [change_end]
  1395.  
  1396. In the examples, it is assumed that the host named CMUC runs the TOPS-20
  1397. operating system, and therefore uses TOPS-20 file system syntax; furthermore,
  1398. an explicit host name is indicated by following the host name with a double
  1399. colon. Remember, however, that namestring syntax is implementation-dependent,
  1400. and this syntax is used here purely for the sake of examples.
  1401.  
  1402. [Function]
  1403. pathname pathname
  1404.  
  1405. The pathname function converts its argument to be a pathname. The argument may
  1406. be a pathname, a string or symbol, or a stream; the result is always a
  1407. pathname.
  1408.  
  1409. [change_begin]
  1410. X3J13 voted in March 1988 not to permit symbols as pathnames (PATHNAME-SYMBOL)
  1411.   and to specify exactly which streams may be used as pathnames
  1412. (PATHNAME-STREAM)   .
  1413.  
  1414. X3J13 voted in January 1989 (CLOSED-STREAM-OPERATIONS)   to specify that
  1415. pathname is unaffected by whether its argument, if a stream, is open or closed.
  1416. X3J13 further commented that because some implementations cannot provide the
  1417. ``true name'' of a file until the file is closed, in such an implementation
  1418. pathname might, in principle, return a different (perhaps more specific) file
  1419. name after the stream is closed. However, such behavior is prohibited; pathname
  1420. must return the same pathname after a stream is closed as it would have while
  1421. the stream was open. See truename.
  1422. [change_end]
  1423.  
  1424. [Function]
  1425. truename pathname
  1426.  
  1427. The truename function endeavors to discover the ``true name'' of the file
  1428. associated with the pathname within the file system. If the pathname is an open
  1429. stream already associated with a file in the file system, that file is used.
  1430. The ``true name'' is returned as a pathname. An error is signaled if an
  1431. appropriate file cannot be located within the file system for the given
  1432. pathname.
  1433.  
  1434. The truename function may be used to account for any file name translations
  1435. performed by the file system, for example.
  1436.  
  1437. For example, suppose that DOC: is a TOPS-20 logical device name that is
  1438. translated by the TOPS-20 file system to be PS:<DOCUMENTATION>.
  1439.  
  1440. (setq file (open "CMUC::DOC:DUMPER.HLP"))
  1441. (namestring (pathname file)) => "CMUC::DOC:DUMPER.HLP"
  1442. (namestring (truename file))
  1443.    => "CMUC::PS:<DOCUMENTATION>DUMPER.HLP.13"
  1444.  
  1445. [change_begin]
  1446. X3J13 voted in March 1988 not to permit symbols as pathnames (PATHNAME-SYMBOL)
  1447.   and to specify exactly which streams may be used as pathnames
  1448. (PATHNAME-STREAM)   .
  1449.  
  1450. X3J13 voted in January 1989 (CLOSED-STREAM-OPERATIONS)   to specify that
  1451. truename may be applied to a stream whether the stream is open or closed. X3J13
  1452. further commented that because some implementations cannot provide the ``true
  1453. name'' of a file until the file is closed, in principle it would be possible in
  1454. such an implementation for truename to return a different file name after the
  1455. stream is closed. Such behavior is permitted; in this respect truename differs
  1456. from pathname.
  1457.  
  1458. X3J13 voted in June 1989 (PATHNAME-WILD)   to clarify that truename accepts
  1459. only non-wild pathnames; an error is signaled if wild-pathname-p would be true
  1460. of the pathname argument.
  1461.  
  1462. X3J13 voted in June 1989 (PATHNAME-LOGICAL)   to require truename to accept
  1463. logical pathnames (see section 23.1.5). However, truename never returns a
  1464. logical pathname.
  1465. [change_end]
  1466.  
  1467. [Function]
  1468. parse-namestring thing &optional host defaults &key :start :end :junk-allowed
  1469.  
  1470. [old_change_begin]
  1471. This turns thing into a pathname. The thing is usually a string (that is, a
  1472. namestring), but it may be a symbol (in which case the print name is used) or a
  1473. pathname or stream (in which case no parsing is needed, but an error check may
  1474. be made for matching hosts).
  1475. [old_change_end]
  1476.  
  1477. [change_begin]
  1478. X3J13 voted in March 1988 not to permit symbols as pathnames (PATHNAME-SYMBOL)
  1479.   and to specify exactly which streams may be used as pathnames
  1480. (PATHNAME-STREAM)   . The thing argument may not be a symbol.
  1481.  
  1482. X3J13 voted in June 1989 (PATHNAME-LOGICAL)   to require parse-namestring to
  1483. accept logical pathname namestrings (see section 23.1.5).
  1484. [change_end]
  1485.  
  1486. This function does not, in general, do defaulting of pathname components, even
  1487. though it has an argument named defaults; it only does parsing. The host and
  1488. defaults arguments are present because in some implementations it may be that a
  1489. namestring can only be parsed with reference to a particular file name syntax
  1490. of several available in the implementation. If host is non-nil, it must be a
  1491. host name that could appear in the host component of a pathname, or nil; if
  1492. host is nil then the host name is extracted from the default pathname in
  1493. defaults and used to determine the syntax convention. The defaults argument
  1494. defaults to the value of *default-pathname-defaults*.
  1495.  
  1496. For a string (or symbol) argument, parse-namestring parses a file name within
  1497. it in the range delimited by the :start and :end arguments (which are integer
  1498. indices into string, defaulting to the beginning and end of the string).
  1499.  
  1500. [change_begin]
  1501. See chapter 14 for a discussion of :start and :end arguments.
  1502. [change_end]
  1503.  
  1504. If :junk-allowed is not nil, then the first value returned is the pathname
  1505. parsed, or nil if no syntactically correct pathname was seen.
  1506.  
  1507. If :junk-allowed is nil (the default), then the entire substring is scanned.
  1508. The returned value is the pathname parsed. An error is signaled if the
  1509. substring does not consist entirely of the representation of a pathname,
  1510. possibly surrounded on either side by whitespace characters if that is
  1511. appropriate to the cultural conventions of the implementation.
  1512.  
  1513. In either case, the second value is the index into the string of the delimiter
  1514. that terminated the parse, or the index beyond the substring if the parse
  1515. terminated at the end of the substring (as will always be the case if
  1516. :junk-allowed is false).
  1517.  
  1518. If thing is not a string or symbol, then start (which defaults to zero in any
  1519. case) is always returned as the second value.
  1520.  
  1521. Parsing an empty string always succeeds, producing a pathname with all
  1522. components (except the host) equal to nil.
  1523.  
  1524. Note that if host is specified and not nil, and thing contains a manifest host
  1525. name, an error is signaled if the hosts do not match.
  1526.  
  1527. If thing contains an explicit host name and no explicit device name, then it
  1528. might be appropriate, depending on the implementation environment, for
  1529. parse-namestring to supply the standard default device for that host as the
  1530. device component of the resulting pathname.
  1531.  
  1532. [Function]
  1533. merge-pathnames pathname &optional defaults default-version
  1534.  
  1535. [old_change_begin]
  1536. This is the function that most programs should call to process a file name
  1537. supplied by the user. It fills in unspecified components of pathname from the
  1538. defaults, and returns a new pathname. The pathname and defaults arguments may
  1539. each be a pathname, stream, string, or symbol. The result is always a pathname.
  1540.  
  1541. [old_change_end]
  1542.  
  1543. [change_begin]
  1544. X3J13 voted in March 1988 not to permit symbols as pathnames (PATHNAME-SYMBOL)
  1545.   and to specify exactly which streams may be used as pathnames
  1546. (PATHNAME-STREAM)   .
  1547.  
  1548. X3J13 voted in June 1989 (PATHNAME-LOGICAL)   to require merge-namestrings to
  1549. recognize a logical pathname namestring as its first argument if its second
  1550. argument is a logical pathname (see section 23.1.5).
  1551.  
  1552. X3J13 voted in January 1989 (CLOSED-STREAM-OPERATIONS)   to specify that
  1553. merge-pathname is unaffected by whether the first argument, if a stream, is
  1554. open or closed. If the first argument is a stream, merge-pathname behaves as if
  1555. the function pathname were applied to the stream and the resulting pathname
  1556. used instead.
  1557.  
  1558. X3J13 voted in June 1989 (PATHNAME-COMPONENT-CASE)   to require merge-pathnames
  1559. to map customary case in argument pathnames to the customary case in returned
  1560. pathnames (see section 23.1.2).
  1561. [change_end]
  1562.  
  1563. defaults defaults to the value of *default-pathname-defaults*.
  1564.  
  1565. default-version defaults to :newest.
  1566.  
  1567. Here is an example of the use of merge-pathnames:
  1568.  
  1569. (merge-pathnames "CMUC::FORMAT"
  1570.                  "CMUC::PS:<LISPIO>.FASL")
  1571.    => a pathname object that re-expressed as a namestring would be
  1572.       "CMUC::PS:<LISPIO>FORMAT.FASL.0"
  1573.  
  1574. Defaulting of pathname components is done by filling in components taken from
  1575. another pathname. This is especially useful for cases such as a program that
  1576. has an input file and an output file, and asks the user for the name of both,
  1577. letting the unsupplied components of one name default from the other.
  1578. Unspecified components of the output pathname will come from the input
  1579. pathname, except that the type should default not to the type of the input but
  1580. to the appropriate default type for output from this program.
  1581.  
  1582. The pathname merging operation takes as input a given pathname, a defaults
  1583. pathname, and a default version, and returns a new pathname. Basically, the
  1584. missing components in the given pathname are filled in from the defaults
  1585. pathname, except that if no version is specified the default version is used.
  1586. The default version is usually :newest; if no version is specified the newest
  1587. version in existence should be used. The default version can be nil, to
  1588. preserve the information that it was missing in the input pathname.
  1589.  
  1590. If the given pathname explicitly specifies a host and does not supply a device,
  1591. then if the host component of the defaults matches the host component of the
  1592. given pathname, then the device is taken from the defaults; otherwise the
  1593. device will be the default file device for that host. Next, if the given
  1594. pathname does not specify a host, device, directory, name, or type, each such
  1595. component is copied from the defaults. The merging rules for the version are
  1596. more complicated and depend on whether the pathname specifies a name. If the
  1597. pathname doesn't specify a name, then the version, if not provided, will come
  1598. from the defaults, just like the other components. However, if the pathname
  1599. does specify a name, then the version is not affected by the defaults. The
  1600. reason is that the version ``belongs to'' some other file name and is unlikely
  1601. to have anything to do with the new one. Finally, if this process leaves the
  1602. version missing, the default version is used.
  1603.  
  1604. The net effect is that if the user supplies just a name, then the host, device,
  1605. directory, and type will come from the defaults, but the version will come from
  1606. the default version argument to the merging operation. If the user supplies
  1607. nothing, or just a directory, the name, type, and version will come over from
  1608. the defaults together. If the host's file name syntax provides a way to input a
  1609. version without a name or type, the user can let the name and type default but
  1610. supply a version different from the one in the defaults.
  1611.  
  1612. [change_begin]
  1613. X3J13 voted in June 1989 (PATHNAME-SYNTAX-ERROR-TIME)   to agree to disagree:
  1614. merge-pathname might or might not perform plausibility checking on its
  1615. arguments to ensure that the resulting pathname can be converted a valid
  1616. namestring. User beware: this could cause portability problems.
  1617.  
  1618. For example, suppose that host LOSER constrains file types to be three
  1619. characters or fewer but host CMUC does not. Then "LOSER::FORMAT" is a valid
  1620. namestring and "CMUC::PS:<LISPIO>.FASL" is a valid namestring, but
  1621.  
  1622. (merge-pathnames "LOSER::FORMAT" "CMUC::PS:<LISPIO>.FASL")
  1623.  
  1624. might signal an error in some implementations because the hypothetical result
  1625. would be a pathname equivalent to the namestring "LOSER::FORMAT.FASL" which is
  1626. illegal because the file type FASL has more than three characters. In other
  1627. implementations merge-pathname might return a pathname but that pathname might
  1628. cause namestring to signal an error.
  1629. [change_end]
  1630.  
  1631. [Variable]
  1632. *default-pathname-defaults*
  1633.  
  1634. This is the default pathname-defaults pathname; if any pathname primitive that
  1635. needs a set of defaults is not given one, it uses this one. As a general rule,
  1636. however, each program should have its own pathname defaults rather than using
  1637. this one.
  1638.  
  1639. [old_change_begin]
  1640.  
  1641. [Function]
  1642. make-pathname &key :host :device :directory :name :type :version :defaults
  1643.  
  1644. Given some components, make-pathname constructs and returns a pathname. After
  1645. the components specified explicitly by the :host, :device, :directory, :name,
  1646. :type, and :version arguments are filled in, the merging rules used by
  1647. merge-pathnames are used to fill in any missing components from the defaults
  1648. specified by the :defaults argument. The default value of the :defaults
  1649. argument is a pathname whose host component is the same as the host component
  1650. of the value of *default-pathname-defaults*, and whose other components are all
  1651. nil.
  1652.  
  1653. Whenever a pathname is constructed, whether by make-pathname or some other
  1654. function, the components may be canonicalized if appropriate. For example, if a
  1655. file system is insensitive to case, then alphabetic characters may be forced to
  1656. be all uppercase or all lowercase by the implementation.
  1657. [old_change_end]
  1658.  
  1659. [change_begin]
  1660. The following example assumes the use of UNIX syntax and conventions.
  1661.  
  1662. (make-pathname :host "technodrome"
  1663.                :directory '(:absolute "usr" "krang")
  1664.                :name "shredder")
  1665.   => #P"technodrome:/usr/krang/shredder"
  1666.  
  1667. X3J13 voted in June 1989 (PATHNAME-COMPONENT-CASE)   to add a new keyword
  1668. argument :case to make-pathname. The new argument description is therefore as
  1669. follows:
  1670.  
  1671. [Function]
  1672. make-pathname &key :host :device :directory :name :type :version :defaults
  1673. :case
  1674.  
  1675. See section 23.1.2 for a description of the :case argument.
  1676.  
  1677. X3J13 voted in June 1989 (PATHNAME-SYNTAX-ERROR-TIME)   to agree to disagree:
  1678. make-pathname might or might not check on its arguments to ensure that the
  1679. resulting pathname can be converted to a valid namestring. If make-pathname
  1680. does not check its arguments and signal an error in problematical cases,
  1681. namestring yet might or might not signal an error when given the resulting
  1682. pathname. User beware: this could cause portability problems.
  1683. [change_end]
  1684.  
  1685. [Function]
  1686. pathnamep object
  1687.  
  1688. This predicate is true if object is a pathname, and otherwise is false.
  1689.  
  1690. (pathnamep x) == (typep x 'pathname)
  1691.  
  1692. [old_change_begin]
  1693.  
  1694. [Function]
  1695. pathname-host pathname
  1696. pathname-device pathname
  1697. pathname-directory pathname
  1698. pathname-name pathname
  1699. pathname-type pathname
  1700. pathname-version pathname
  1701.  
  1702. These return the components of the argument pathname, which may be a pathname,
  1703. string or symbol, or stream. The returned values can be strings, special
  1704. symbols, or some other object in the case of structured components. The type
  1705. will always be a string or a symbol. The version will always be a number or a
  1706. symbol.
  1707. [old_change_end]
  1708.  
  1709. [change_begin]
  1710. X3J13 voted in March 1988 not to permit symbols as pathnames (PATHNAME-SYMBOL)
  1711.   and to specify exactly which streams may be used as pathnames
  1712. (PATHNAME-STREAM)   .
  1713.  
  1714. X3J13 voted in January 1989 (CLOSED-STREAM-OPERATIONS)   to specify that these
  1715. operations are unaffected by whether the first argument, if a stream, is open
  1716. or closed. If the first argument is a stream, each operation behaves as if the
  1717. function pathname were applied to the stream and the resulting pathname used
  1718. instead.
  1719.  
  1720. X3J13 voted in June 1989 (PATHNAME-COMPONENT-CASE)   to add a keyword argument
  1721. :case to all of the pathname accessor functions except pathname-version. The
  1722. new argument descriptions are therefore as follows:
  1723.  
  1724. [Function]
  1725. pathname-host pathname &key :case
  1726. pathname-device pathname &key :case
  1727. pathname-directory pathname &key :case
  1728. pathname-name pathname &key :case
  1729. pathname-type pathname &key :case
  1730. pathname-version pathname
  1731.  
  1732. See section 23.1.2 for a description of the :case argument.
  1733.  
  1734. X3J13 voted in June 1989 (PATHNAME-SUBDIRECTORY-LIST)   to specify that
  1735. pathname-directory always returns nil, :unspecific, or a list-never a string,
  1736. never :wild (see section 23.1.3). If a list is returned, it is not guaranteed
  1737. to be freshly consed; the consequences of modifying this list are undefined.
  1738. [change_end]
  1739.  
  1740. [Function]
  1741. namestring pathname
  1742. file-namestring pathname
  1743. directory-namestring pathname
  1744. host-namestring pathname
  1745. enough-namestring pathname &optional defaults
  1746.  
  1747. The pathname argument may be a pathname, a string or symbol, or a stream that
  1748. is or was open to a file. The name represented by pathname is returned as a
  1749. namelist in canonical form.
  1750.  
  1751. If pathname is a stream, the name returned represents the name used to open the
  1752. file, which may not be the actual name of the file (see truename).
  1753.  
  1754. [change_begin]
  1755. X3J13 voted in March 1988 not to permit symbols as pathnames (PATHNAME-SYMBOL)
  1756.   and to specify exactly which streams may be used as pathnames
  1757. (PATHNAME-STREAM)   .
  1758.  
  1759. X3J13 voted in January 1989 (CLOSED-STREAM-OPERATIONS)   to specify that these
  1760. operations are unaffected by whether the first argument, if a stream, is open
  1761. or closed. If the first argument is a stream, each operation behaves as if the
  1762. function pathname were applied to the stream and the resulting pathname used
  1763. instead.
  1764. [change_end]
  1765.  
  1766. namestring returns the full form of the pathname as a string. file-namestring
  1767. returns a string representing just the name, type, and version components of
  1768. the pathname; the result of directory-namestring represents just the
  1769. directory-name portion; and host-namestring returns a string for just the
  1770. host-name portion. Note that a valid namestring cannot necessarily be
  1771. constructed simply by concatenating some of the three shorter strings in some
  1772. order.
  1773.  
  1774. enough-namestring takes another argument, defaults. It returns an abbreviated
  1775. namestring that is just sufficient to identify the file named by pathname when
  1776. considered relative to the defaults (which defaults to the value of
  1777. *default-pathname-defaults*). That is, it is required that
  1778.  
  1779. (merge-pathnames (enough-namestring pathname defaults) defaults) ==
  1780.  (merge-pathnames (parse-namestring pathname nil defaults) defaults)
  1781.  
  1782. in all cases; and the result of enough-namestring is, roughly speaking, the
  1783. shortest reasonable string that will still satisfy this criterion.
  1784.  
  1785. [change_begin]
  1786. X3J13 voted in June 1989 (PATHNAME-SYNTAX-ERROR-TIME)   to agree to disagree:
  1787. make-pathname and merge-pathnames might or might not be able to produce
  1788. pathnames that cannot be converted to valid namestrings. User beware: this
  1789. could cause portability problems.
  1790. [change_end]
  1791.  
  1792. [Function]
  1793. user-homedir-pathname &optional host
  1794.  
  1795. Returns a pathname for the user's ``home directory'' on host. The host argument
  1796. defaults in some appropriate implementation-dependent manner. The concept of
  1797. ``home directory'' is itself somewhat implementation-dependent, but from the
  1798. point of view of Common Lisp it is the directory where the user keeps personal
  1799. files such as initialization files and mail. If it is impossible to determine
  1800. this information, then nil is returned instead of a pathname; however,
  1801. user-homedir-pathname never returns nil if the host argument is not specified.
  1802. This function returns a pathname without any name, type, or version component
  1803. (those components are all nil).
  1804.  
  1805. -------------------------------------------------------------------------------
  1806.  
  1807. 23.2. Opening and Closing Files
  1808.  
  1809. When a file is opened, a stream object is constructed to serve as the file
  1810. system's ambassador to the Lisp environment; operations on the stream are
  1811. reflected by operations on the file in the file system. The act of closing the
  1812. file (actually, the stream) ends the association; the transaction with the file
  1813. system is terminated, and input/output may no longer be performed on the
  1814. stream. The stream function close may be used to close a file; the functions
  1815. described below may be used to open them. The basic operation is open, but
  1816. with-open-file is usually more convenient for most applications.
  1817.  
  1818. [Function]
  1819. open filename &key :direction :element-type :if-exists :if-does-not-exist
  1820. :external-format
  1821.  
  1822. [change_begin]
  1823. X3J13 voted in June 1989 (MORE-CHARACTER-PROPOSAL)   to add to the function
  1824. open a new keyword argument :external-format. This argument did not appear in
  1825. the preceding argument description in the first edition.
  1826. [change_end]
  1827.  
  1828. This returns a stream that is connected to the file specified by filename. The
  1829. filename is the name of the file to be opened; it may be a string, a pathname,
  1830. or a stream. (If the filename is a stream, then it is not closed first or
  1831. otherwise affected; it is used merely to provide a file name for the opening of
  1832. a new stream.)
  1833.  
  1834. [change_begin]
  1835. X3J13 voted in January 1989 (STREAM-ACCESS)   to specify that the result of
  1836. open, if it is a stream, is always a stream of type file-stream.
  1837.  
  1838. X3J13 voted in March 1988 (PATHNAME-STREAM)   to specify exactly which streams
  1839. may be used as pathnames. See section 23.1.6.
  1840.  
  1841. X3J13 voted in January 1989 (CLOSED-STREAM-OPERATIONS)   to specify that open
  1842. is unaffected by whether the first argument, if a stream, is open or closed. If
  1843. the first argument is a stream, open behaves as if the function pathname were
  1844. applied to the stream and the resulting pathname used instead.
  1845.  
  1846. X3J13 voted in June 1989 (PATHNAME-WILD)   to clarify that open accepts only
  1847. non-wild pathnames; an error is signaled if wild-pathname-p would be true of
  1848. filename.
  1849.  
  1850. X3J13 voted in June 1989 (PATHNAME-LOGICAL)   to require open to accept logical
  1851. pathnames (see section 23.1.5).
  1852. [change_end]
  1853.  
  1854. The keyword arguments specify what kind of stream to produce and how to handle
  1855. errors:
  1856.  
  1857. :direction
  1858.      This argument specifies whether the stream should handle input, output, or
  1859.      both.
  1860.  
  1861.           :input
  1862.                The result will be an input stream. This is the default.
  1863.  
  1864.           :output
  1865.                The result will be an output stream.
  1866.  
  1867.           :io  The result will be a bidirectional stream.
  1868.  
  1869.           :probe
  1870.                The result will be a no-directional stream (in effect, the
  1871.                stream is created and then closed). This is useful for
  1872.                determining whether a file exists without actually setting
  1873.                up a complete stream.
  1874.  
  1875. :element-type
  1876.      This argument specifies the type of the unit of transaction for the
  1877.      stream. Anything that can be recognized as being a finite subtype of
  1878.      character or integer is acceptable. In particular, the following types are
  1879.      recognized:
  1880.  
  1881.      [old_change_begin]
  1882.  
  1883.           string-char
  1884.                The unit of transaction is a string-character. The
  1885.                functions read-char and/or write-char may be used on the
  1886.                stream. This is the default.
  1887.  
  1888.           character
  1889.                The unit of transaction is any character, not just a
  1890.                string-character. The functions read-char and/or write-char
  1891.                may be used on the stream.
  1892.  
  1893.      [old_change_end]
  1894.  
  1895.      [change_begin]
  1896.      X3J13 voted in June 1989 (MORE-CHARACTER-PROPOSAL)   to eliminate the type
  1897.      string-char, add the type base-character, and redefine open to use the
  1898.      type character as the default :element-type.
  1899.  
  1900.      The preceding two possibilities should therefore be replaced by the
  1901.      following.
  1902.  
  1903.           character
  1904.                The unit of transaction is any character, not just a
  1905.                string-character. The functions read-char and write-char
  1906.                (depending on the value of the :direction argument) may be
  1907.                used on the stream. This is the default.
  1908.  
  1909.           base-character
  1910.                The unit of transaction is a base character. The functions
  1911.                read-char and write-char (depending on the value of the
  1912.                :direction argument) may be used on the stream.
  1913.  
  1914.      [change_end]
  1915.  
  1916.           (unsigned-byte n)
  1917.                The unit of transaction is an unsigned byte (a non-negative
  1918.                integer) of size n. The functions read-byte and/or
  1919.                write-byte may be used on the stream.
  1920.  
  1921.           unsigned-byte
  1922.                The unit of transaction is an unsigned byte (a non-negative
  1923.                integer); the size of the byte is determined by the file
  1924.                system. The functions read-byte and/or write-byte may be
  1925.                used on the stream.
  1926.  
  1927.           (signed-byte n)
  1928.                The unit of transaction is a signed byte of size n. The
  1929.                functions read-byte and/or write-byte may be used on the
  1930.                stream.
  1931.  
  1932.           signed-byte
  1933.                The unit of transaction is a signed byte; the size of the
  1934.                byte is determined by the file system. The functions
  1935.                read-byte and/or write-byte may be used on the stream.
  1936.  
  1937.           bit  The unit of transaction is a bit (values 0 and 1). The
  1938.                functions read-byte and/or write-byte may be used on the
  1939.                stream.
  1940.  
  1941.           (mod n)
  1942.                The unit of transaction is a non-negative integer less than
  1943.                n. The functions read-byte and/or write-byte may be used on
  1944.                the stream.
  1945.  
  1946.           :default
  1947.                The unit of transaction is to be determined by the file
  1948.                system, based on the file it finds. The type can be
  1949.                determined by using the function stream-element-type.
  1950.  
  1951. :if-exists
  1952.      This argument specifies the action to be taken if the :direction is
  1953.      :output or :io and a file of the specified name already exists. If the
  1954.      direction is :input or :probe, this argument is ignored.
  1955.  
  1956.           :error
  1957.                Signals an error. This is the default when the version
  1958.                component of the filename is not :newest.
  1959.  
  1960.           :new-version
  1961.                Creates a new file with the same file name but with a
  1962.                larger version number. This is the default when the version
  1963.                component of the filename is :newest.
  1964.  
  1965.           :rename
  1966.                Renames the existing file to some other name and then
  1967.                creates a new file with the specified name.
  1968.  
  1969.           :rename-and-delete
  1970.                Renames the existing file to some other name and then
  1971.                deletes it (but does not expunge it, on those systems that
  1972.                distinguish deletion from expunging). Then create a new
  1973.                file with the specified name.
  1974.  
  1975.           :overwrite
  1976.                Uses the existing file. Output operations on the stream
  1977.                will destructively modify the file. If the :direction is
  1978.                :io, the file is opened in a bidirectional mode that allows
  1979.                both reading and writing. The file pointer is initially
  1980.                positioned at the beginning of the file; however, the file
  1981.                is not truncated back to length zero when it is opened.
  1982.                This mode is most useful when the file-position function
  1983.                can be used on the stream.
  1984.  
  1985.           :append
  1986.                Uses the existing file. Output operations on the stream
  1987.                will destructively modify the file. The file pointer is
  1988.                initially positioned at the end of the file. If the
  1989.                :direction is :io, the file is opened in a bidirectional
  1990.                mode that allows both reading and writing.
  1991.  
  1992.           :supersede
  1993.                Supersedes the existing file. If possible, the
  1994.                implementation should arrange not to destroy the old file
  1995.                until the new stream is closed, against the possibility
  1996.                that the stream will be closed in ``abort'' mode (see
  1997.                close). This differs from :new-version in that :supersede
  1998.                creates a new file with the same name as the old one,
  1999.                rather than a file name with a higher version number.
  2000.  
  2001.           nil  Does not create a file or even a stream, but instead simply
  2002.                returns nil to indicate failure.
  2003.  
  2004.      If the :direction is :output or :io and the value of :if-exists is
  2005.      :new-version, then the version of the (newly created) file that is opened
  2006.      will be a version greater than that of any other file in the file system
  2007.      whose other pathname components are the same as those of filename.
  2008.  
  2009.      If the :direction is :input or :probe or the value of :if-exists is not
  2010.      :new-version, and the version component of the filename is :newest, then
  2011.      the file opened is that file already existing in the file system that has
  2012.      a version greater than that of any other file in the file system whose
  2013.      other pathname components are the same as those of filename.
  2014.  
  2015.      [change_begin]
  2016.      Some file systems permit yet other actions to be taken when a file already
  2017.      exists; therefore, some implementations provide implementation-specific
  2018.      :if-exist options.
  2019.      [change_end]
  2020.  
  2021. -------------------------------------------------------------------------------
  2022. Implementation note: The various file systems in existence today have widely
  2023. differing capabilities. A given implementation may not be able to support all
  2024. of these options in exactly the manner stated. An implementation is required to
  2025. recognize all of these option keywords and to try to do something
  2026. ``reasonable'' in the context of the host operating system. Implementors are
  2027. encouraged to approximate the semantics specified here as closely as possible.
  2028.  
  2029. As an example, suppose that a file system does not support distinct file
  2030. versions and does not distinguish the notions of deletion and expunging (in
  2031. some file systems file deletion is reversible until an expunge operation is
  2032. performed). Then :new-version might be treated the same as :rename or
  2033. :supersede, and :rename-and-delete might be treated the same as :supersede.
  2034.  
  2035. If it is utterly impossible for an implementation to handle some option in a
  2036. manner close to what is specified here, it may simply signal an error. The
  2037. opening of files is an area where complete portability is too much to hope for;
  2038. the intent here is simply to make things as portable as possible by providing
  2039. specific names for a range of commonly supportable options.
  2040. -------------------------------------------------------------------------------
  2041.  
  2042. :if-does-not-exist
  2043.      This argument specifies the action to be taken if a file of the specified
  2044.      name does not already exist.
  2045.  
  2046.           :error
  2047.                Signals an error. This is the default if the :direction is
  2048.                :input, or if the :if-exists argument is :overwrite or
  2049.                :append.
  2050.  
  2051.           :create
  2052.                Creates an empty file with the specified name and then
  2053.                proceeds as if it had already existed (but do not perform
  2054.                any processing directed by the :if-exists argument). This
  2055.                is the default if the :direction is :output or :io, and the
  2056.                :if-exists argument is anything but :overwrite or :append.
  2057.  
  2058.           nil  Does not create a file or even a stream, but instead simply
  2059.                returns nil to indicate failure. This is the default if the
  2060.                :direction is :probe.
  2061.  
  2062. [change_begin]
  2063. X3J13 voted in June 1989 (MORE-CHARACTER-PROPOSAL)   to add to the function
  2064. open a new keyword argument :external-format.
  2065.  
  2066. :external-format
  2067.      This argument specifies an implementation-recognized scheme for
  2068.      representing characters in files. The default value is :default and is
  2069.      implementation-defined but must support the base characters. An error is
  2070.      signaled if the implementation does recognize the specified format.
  2071.  
  2072.      This argument may be specified if the :direction argument is :input,
  2073.      :output, or :io. It is an error to write a character to the resulting
  2074.      stream that cannot be represented by the specified file format. (However,
  2075.      the #¥Newline character cannot produce such an error; implementations must
  2076.      provide appropriate line division behavior for all character streams.)
  2077.  
  2078.      See stream-external-format.
  2079.  
  2080. [change_end]
  2081.  
  2082. When the caller is finished with the stream, it should close the file by using
  2083. the close function. The with-open-file form does this automatically, and so is
  2084. preferred for most purposes. open should be used only when the control
  2085. structure of the program necessitates opening and closing of a file in some way
  2086. more complex than provided by with-open-file. It is suggested that any program
  2087. that uses open directly should use the special form unwind-protect to close the
  2088. file if an abnormal exit occurs.
  2089.  
  2090. [Macro]
  2091.  
  2092. with-open-file (stream filename {options}*)
  2093.        {declaration}* {form}*
  2094.  
  2095. with-open-file evaluates the forms of the body (an implicit progn) with the
  2096. variable stream bound to a stream that reads or writes the file named by the
  2097. value of filename. The options are evaluated and are used as keyword arguments
  2098. to the function open.
  2099.  
  2100. When control leaves the body, either normally or abnormally (such as by use of
  2101. throw), the file is automatically closed. If a new output file is being
  2102. written, and control leaves abnormally, the file is aborted and the file system
  2103. is left, so far as possible, as if the file had never been opened. Because
  2104. with-open-file always closes the file, even when an error exit is taken, it is
  2105. preferred over open for most applications.
  2106.  
  2107. filename is the name of the file to be opened; it may be a string, a pathname,
  2108. or a stream.
  2109.  
  2110. [change_begin]
  2111. X3J13 voted in March 1988 (PATHNAME-STREAM)   to specify exactly which streams
  2112. may be used as pathnames. See section 23.1.6.
  2113.  
  2114. X3J13 voted in June 1989 (PATHNAME-WILD)   to clarify that with-open-file
  2115. accepts only non-wild pathnames; an error is signaled if wild-pathname-p would
  2116. be true of the filename argument.
  2117.  
  2118. X3J13 voted in June 1989 (PATHNAME-LOGICAL)   to require with-open-file to
  2119. accept logical pathnames (see section 23.1.5).
  2120. [change_end]
  2121.  
  2122. For example:
  2123.  
  2124. (with-open-file (ifile name
  2125.                  :direction :input)
  2126.   (with-open-file (ofile (merge-pathname-defaults ifile
  2127.                                                   nil
  2128.                                                   "out")
  2129.                          :direction :output
  2130.                          :if-exists :supersede)
  2131.     (transduce-file ifile ofile)))
  2132.  
  2133. [change_begin]
  2134. X3J13 voted in June 1989 (WITH-OPEN-FILE-DOES-NOT-EXIST)   to specify that the
  2135. variable stream is not always bound to a stream; rather it is bound to whatever
  2136. would be returned by a call to open. For example, if the options include
  2137. :if-does-not-exist nil, stream will be bound to nil if the file does not exist.
  2138. In this case the value of stream should be tested within the body of the
  2139. with-open-file form before it is used as a stream. For example:
  2140.  
  2141. (with-open-file (ifile name
  2142.                  :direction :input
  2143.                  :if-does-not-exist nil)
  2144.   ;; Process the file only if it actually exists.
  2145.   (when (streamp name)
  2146.     (compile-cobol-program ifile)))
  2147.  
  2148. [change_end]
  2149.  
  2150. -------------------------------------------------------------------------------
  2151. Implementation note: While with-open-file tries to automatically close the
  2152. stream on exit from the construct, for robustness it is helpful if the garbage
  2153. collector can detect discarded streams and automatically close them.
  2154. -------------------------------------------------------------------------------
  2155.  
  2156. -------------------------------------------------------------------------------
  2157.  
  2158. 23.3. Renaming, Deleting, and Other File Operations
  2159.  
  2160. These functions provide a standard interface to operations provided in some
  2161. form by most file systems. It may be that some implementations of Common Lisp
  2162. cannot support them all completely.
  2163.  
  2164. [Function]
  2165. rename-file file new-name
  2166.  
  2167. The specified file is renamed to new-name (which must be a file name). The file
  2168. may be a string, a pathname, or a stream. If it is an open stream associated
  2169. with a file, then the stream itself and the file associated with it are
  2170. affected (if the file system permits).
  2171.  
  2172. [change_begin]
  2173. X3J13 voted in March 1988 (PATHNAME-STREAM)   to specify exactly which streams
  2174. may be used as pathnames. See section 23.1.6.
  2175. [change_end]
  2176.  
  2177. rename-file returns three values if successful. The first value is the new-name
  2178. with any missing components filled in by performing a merge-pathnames operation
  2179. using file as the defaults. The second value is the truename of the file before
  2180. it was renamed. The third value is the truename of the file after it was
  2181. renamed.
  2182.  
  2183. If the renaming operation is not successful, an error is signaled.
  2184.  
  2185. [old_change_begin]
  2186. It is an error to specify a file name containing a :wild component, for file to
  2187. contain a nil component where the file system does not permit a nil component,
  2188. or for the result of defaulting missing components of new-name from file to
  2189. contain a nil component where the file system does not permit a nil component.
  2190. [old_change_end]
  2191.  
  2192. [change_begin]
  2193. X3J13 voted in June 1989 (PATHNAME-WILD)   to specify that supplying a wild
  2194. pathname as the file argument to rename-file has implementation-dependent
  2195. consequences; rename-file might signal an error, for example, or might rename
  2196. all files that match the wild pathname.
  2197.  
  2198. X3J13 voted in June 1989 (PATHNAME-LOGICAL)   to require rename-file to accept
  2199. logical pathnames (see section 23.1.5).
  2200. [change_end]
  2201.  
  2202. -------------------------------------------------------------------------------
  2203. Compatibility note: This corresponds to the function called renamef in MacLisp
  2204. and Lisp Machine Lisp. The name renamef is not used in Common Lisp because the
  2205. convention that a trailing f means ``file'' conflicts with the use of a
  2206. trailing f for forms related to setf.
  2207. -------------------------------------------------------------------------------
  2208.  
  2209. [Function]
  2210. delete-file file
  2211.  
  2212. The specified file is deleted. The file may be a string, a pathname, or a
  2213. stream. If it is an open stream associated with a file, then the stream itself
  2214. and the file associated with it are affected (if the file system permits), in
  2215. which case the stream may or may not be closed immediately, and the deletion
  2216. may be immediate or delayed until the stream is explicitly closed, depending on
  2217. the requirements of the file system.
  2218.  
  2219. [change_begin]
  2220. X3J13 voted in March 1988 (PATHNAME-STREAM)   to specify exactly which streams
  2221. may be used as pathnames. See section 23.1.6.
  2222. [change_end]
  2223.  
  2224. [old_change_begin]
  2225. delete-file returns a non-nil value if successful. It is left to the discretion
  2226. of the implementation whether an attempt to delete a non-existent file is
  2227. considered to be successful. If the deleting operation is not successful, an
  2228. error is signaled.
  2229.  
  2230. It is an error to specify a file name that contains a :wild component or one
  2231. that contains a nil component where the file system does not permit a nil
  2232. component.
  2233. [old_change_end]
  2234.  
  2235. [change_begin]
  2236. X3J13 voted in June 1989 (PATHNAME-WILD)   to clarify that supplying a wild
  2237. pathname as the file argument to delete-file has implementation-dependent
  2238. consequences; delete-file might signal an error, for example, or might delete
  2239. all files that match the wild pathname.
  2240.  
  2241. X3J13 voted in June 1989 (PATHNAME-LOGICAL)   to require delete-file to accept
  2242. logical pathnames (see section 23.1.5).
  2243. [change_end]
  2244.  
  2245. -------------------------------------------------------------------------------
  2246. Compatibility note: This corresponds to the function called deletef in MacLisp
  2247. and Lisp Machine Lisp.
  2248. -------------------------------------------------------------------------------
  2249.  
  2250. [Function]
  2251. probe-file file
  2252.  
  2253. This predicate is false if there is no file named file, and otherwise returns a
  2254. pathname that is the true name of the file (which may be different from file
  2255. because of file links, version numbers, or other artifacts of the file system).
  2256. Note that if the file is an open stream associated with a file, then probe-file
  2257. cannot return nil but will produce the true name of the associated file. See
  2258. truename and the :probe value for the :direction argument to open.
  2259.  
  2260. This corresponds to the function called probef in MacLisp and Lisp Machine
  2261. Lisp.
  2262.  
  2263. [change_begin]
  2264. X3J13 voted in March 1988 (PATHNAME-STREAM)   to specify exactly which streams
  2265. may be used as pathnames. See section 23.1.6.
  2266.  
  2267. X3J13 voted in June 1989 (PATHNAME-WILD)   to clarify that probe-file accepts
  2268. only non-wild pathnames; an error is signaled if wild-pathname-p would be true
  2269. of the file argument.
  2270.  
  2271. X3J13 voted in June 1989 (PATHNAME-LOGICAL)   to require probe-file to accept
  2272. logical pathnames (see section 23.1.5). However, probe-file never returns a
  2273. logical pathname.
  2274.  
  2275. X3J13 voted in January 1989 (CLOSED-STREAM-OPERATIONS)   to specify that
  2276. probe-file is unaffected by whether the first argument, if a stream, is open or
  2277. closed. If the first argument is a stream, probe-file behaves as if the
  2278. function pathname were applied to the stream and the resulting pathname used
  2279. instead. However, X3J13 further commented that the treatment of open streams
  2280. may differ considerably from one implementation to another; for example, in
  2281. some operating systems open files are written under a temporary or invisible
  2282. name and later renamed when closed. In general, programmers writing code
  2283. intended to be portable should be very careful when using probe-file.
  2284. [change_end]
  2285.  
  2286. [Function]
  2287. file-write-date file
  2288.  
  2289. file can be a file name or a stream that is open to a file. This returns the
  2290. time at which the file was created or last written as an integer in universal
  2291. time format (see section 25.4.1), or nil if this cannot be determined.
  2292.  
  2293. [change_begin]
  2294. X3J13 voted in March 1988 (PATHNAME-STREAM)   to specify exactly which streams
  2295. may be used as pathnames. See section 23.1.6.
  2296.  
  2297. X3J13 voted in June 1989 (PATHNAME-WILD)   to clarify that file-write-date
  2298. accepts only non-wild pathnames; an error is signaled if wild-pathname-p would
  2299. be true of the file argument.
  2300.  
  2301. X3J13 voted in June 1989 (PATHNAME-LOGICAL)   to require file-write-date to
  2302. accept logical pathnames (see section 23.1.5).
  2303. [change_end]
  2304.  
  2305. [Function]
  2306. file-author file
  2307.  
  2308. file can be a file name or a stream that is open to a file. This returns the
  2309. name of the author of the file as a string, or nil if this cannot be
  2310. determined.
  2311.  
  2312. [change_begin]
  2313. X3J13 voted in March 1988 (PATHNAME-STREAM)   to specify exactly which streams
  2314. may be used as pathnames. See section 23.1.6.
  2315.  
  2316. X3J13 voted in June 1989 (PATHNAME-WILD)   to clarify that file-author accepts
  2317. only non-wild pathnames; an error is signaled if wild-pathname-p would be true
  2318. of the file argument.
  2319.  
  2320. X3J13 voted in June 1989 (PATHNAME-LOGICAL)   to require file-author to accept
  2321. logical pathnames (see section 23.1.5).
  2322. [change_end]
  2323.  
  2324. [Function]
  2325. file-position file-stream &optional position
  2326.  
  2327. file-position returns or sets the current position within a random-access file.
  2328.  
  2329. (file-position file-stream) returns a non-negative integer indicating the
  2330. current position within the file-stream, or nil if this cannot be determined.
  2331. The file position at the start of a file will be zero. The value returned by
  2332. file-position increases monotonically as input or output operations are
  2333. performed. For a character file, performing a single read-char or write-char
  2334. operation may cause the file position to be increased by more than 1 because of
  2335. character-set translations (such as translating between the Common Lisp
  2336. #¥Newline character and an external ASCII carriage-return/line-feed sequence)
  2337. and other aspects of the implementation. For a binary file, every read-byte or
  2338. write-byte operation increases the file position by 1.
  2339.  
  2340. (file-position file-stream position) sets the position within file-stream to be
  2341. position. The position may be an integer, or :start for the beginning of the
  2342. stream, or :end for the end of the stream. If the integer is too large or
  2343. otherwise inappropriate, an error is signaled (the file-length function returns
  2344. the length beyond which file-position may not access). An integer returned by
  2345. file-position of one argument should, in general, be acceptable as a second
  2346. argument for use with the same file. With two arguments, file-position returns
  2347. t if the repositioning was performed successfully, or nil if it was not (for
  2348. example, because the file was not random-access).
  2349.  
  2350. -------------------------------------------------------------------------------
  2351. Implementation note: Implementations that have character files represented as a
  2352. sequence of records of bounded size might choose to encode the file position
  2353. as, for example, record-number*256+character-within-record. This is a valid
  2354. encoding because it increases monotonically as each character is read or
  2355. written, though not necessarily by 1 at each step. An integer might then be
  2356. considered ``inappropriate'' as a second argument to file-position if, when
  2357. decoded into record number and character number, it turned out that the
  2358. specified record was too short for the specified character number.
  2359. -------------------------------------------------------------------------------
  2360. Compatibility note: This corresponds to the function called filepos in MacLisp
  2361. and Lisp Machine Lisp.
  2362. -------------------------------------------------------------------------------
  2363.  
  2364. [Function]
  2365. file-length file-stream
  2366.  
  2367. file-stream must be a stream that is open to a file. The length of the file is
  2368. returned as a non-negative integer, or nil if the length cannot be determined.
  2369. For a binary file, the length is specifically measured in units of the
  2370. :element-type specified when the file was opened (see open).
  2371.  
  2372. -------------------------------------------------------------------------------
  2373. Compatibility note: This corresponds to the function called lengthf in MacLisp
  2374. and Lisp Machine Lisp.
  2375. -------------------------------------------------------------------------------
  2376.  
  2377. [change_begin]
  2378.  
  2379. [Function]
  2380. file-string-length file-stream object
  2381.  
  2382. X3J13 voted in June 1989 (MORE-CHARACTER-PROPOSAL)   to add the function
  2383. file-string-length. The object must be a string or a character. The function
  2384. file-string-length returns a non-negative integer that is the difference
  2385. between what the file-position of the file-stream would be after and before
  2386. writing the object to the file-stream, or nil if this difference cannot be
  2387. determined. The value returned may depend on the current state of the
  2388. file-stream; that is, calling file-string-length on the same arguments twice
  2389. may in certain circumstances produce two different integers.
  2390. [change_end]
  2391.  
  2392. -------------------------------------------------------------------------------
  2393.  
  2394. 23.4. Loading Files
  2395.  
  2396. To load a file is to read through the file, evaluating each form in it.
  2397. Programs are typically stored in files containing calls to constructs such as
  2398. defun, defmacro, and defvar, which define the functions and variables of the
  2399. program.
  2400.  
  2401. Loading a compiled (``fasload'') file is similar, except that the file does not
  2402. contain text but rather pre-digested expressions created by the compiler that
  2403. can be loaded more quickly.
  2404.  
  2405. [Function]
  2406. load filename &key :verbose :print :if-does-not-exist
  2407.  
  2408. This function loads the file named by filename into the Lisp environment. It is
  2409. assumed that a text (character file) can be automatically distinguished from an
  2410. object (binary) file by some appropriate implementation-dependent means,
  2411. possibly by the file type. The defaults for filename are taken from the
  2412. variable *default-pathname-defaults*. If the filename (after the merging in of
  2413. the defaults) does not explicitly specify a type, and both text and object
  2414. types of the file are available in the file system, load should try to select
  2415. the more appropriate file by some implementation-dependent means.
  2416.  
  2417. If the first argument is a stream rather than a pathname, then load determines
  2418. what kind of stream it is and loads directly from the stream.
  2419.  
  2420. The :verbose argument (which defaults to the value of *load-verbose*), if true,
  2421. permits load to print a message in the form of a comment (that is, with a
  2422. leading semicolon) to *standard-output* indicating what file is being loaded
  2423. and other useful information.
  2424.  
  2425. [old_change_begin]
  2426. The :print argument (default nil), if true, causes the value of each expression
  2427. loaded to be printed to *standard-output*. If a binary file is being loaded,
  2428. then what is printed may not reflect precisely the contents of the source file,
  2429. but nevertheless some information will be printed.
  2430. [old_change_end]
  2431.  
  2432. [change_begin]
  2433. X3J13 voted in March 1989 (COMPILER-VERBOSITY)   to add the variable
  2434. *load-print*; its value is used as the default for the :print argument to load.
  2435.  
  2436. The function load rebinds *package* to its current value. If some form in the
  2437. file changes the value of *package* during loading, the old value will be
  2438. restored when the loading is completed. (This was specified in the first
  2439. edition under the description of *package*; for convenience I now mention it
  2440. here as well.)
  2441.  
  2442. X3J13 voted in March 1988 (PATHNAME-STREAM)   to specify exactly which streams
  2443. may be used as pathnames. See section 23.1.6.
  2444.  
  2445. X3J13 voted in June 1989 (PATHNAME-WILD)   to clarify that supplying a wild
  2446. pathname as the filename argument to load has implementation-dependent
  2447. consequences; load might signal an error, for example, or might load all files
  2448. that match the pathname.
  2449.  
  2450. X3J13 voted in June 1989 (PATHNAME-LOGICAL)   to require load to accept logical
  2451. pathnames (see section 23.1.5).
  2452. [change_end]
  2453.  
  2454. If a file is successfully loaded, load always returns a non-nil value. If
  2455. :if-does-not-exist is specified and is nil, load just returns nil rather than
  2456. signaling an error if the file does not exist.
  2457.  
  2458. [change_begin]
  2459. X3J13 voted in March 1989 (IN-SYNTAX)   to require that load bind *readtable*
  2460. to its current value at the time load is called; the dynamic extent of the
  2461. binding should encompass all of the file-loading activity. This allows a
  2462. portable program to include forms such as
  2463.  
  2464. (in-package "FOO")
  2465.  
  2466. (eval-when (:execute :load-toplevel :compile-toplevel)
  2467.   (setq *readtable* foo:my-readtable))
  2468.  
  2469. without performing a net global side effect on the loading environment. Such
  2470. statements allow the remainder of such a file to be read either as interpreted
  2471. code or by compile-file in a syntax determined by an alternative readtable.
  2472.  
  2473. X3J13 voted in June 1989 (LOAD-TRUENAME)   to require that load bind two new
  2474. variables *load-pathname* and *load-truename*; the dynamic extent of the
  2475. bindings should encompass all of the file-loading activity.
  2476. [change_end]
  2477.  
  2478. [Variable]
  2479. *load-verbose*
  2480.  
  2481. This variable provides the default for the :verbose argument to load. Its
  2482. initial value is implementation-dependent.
  2483.  
  2484. [change_begin]
  2485.  
  2486. [Variable]
  2487. *load-print*
  2488.  
  2489. X3J13 voted in March 1989 (COMPILER-VERBOSITY)   to add *load-print*. This
  2490. variable provides the default for the :print argument to load. Its initial
  2491. value is nil.
  2492.  
  2493. [Variable]
  2494. *load-pathname*
  2495.  
  2496. X3J13 voted in June 1989 (LOAD-TRUENAME)   to introduce *load-pathname*; it is
  2497. initially nil but load binds it to a pathname that represents the file name
  2498. given as the first argument to load merged with the defaults (see
  2499. merge-pathname).
  2500.  
  2501. [Variable]
  2502. *load-truename*
  2503.  
  2504. X3J13 voted in June 1989 (LOAD-TRUENAME)   to introduce *load-truename*; it is
  2505. initially nil but load binds it to the ``true name'' of the file being loaded.
  2506. See truename.
  2507.  
  2508. X3J13 voted in March 1989 (LOAD-OBJECTS)   to introduce a facility based on the
  2509. Object System whereby a user can specify how compile-file and load must
  2510. cooperate to reconstruct compile-time constant objects at load time. The
  2511. protocol is simply this: compile-file calls the generic function make-load-form
  2512. on any object that is referenced as a constant or as a self-evaluating form, if
  2513. the object's metaclass is standard-class, structure-class, any user-defined
  2514. metaclass (not a subclass of built-in-class), or any of a possibly empty
  2515. implementation-defined list of other metaclasses; compile-file will call
  2516. make-load-form only once for any given object (as determined by eq) within a
  2517. single file. The user-programmability stems from the possibility of
  2518. user-defined methods for make-load-form. The helper function
  2519. make-load-form-saving-slots makes it easy to write commonly used versions of
  2520. such methods.
  2521.  
  2522. [Generic function]
  2523. make-load-form object
  2524.  
  2525. The argument is an object that is referenced as a constant or as a
  2526. self-evaluating form in a file being compiled by compile-file. The objective is
  2527. to enable load to construct an equivalent object.
  2528.  
  2529. The first value, called the creation form, is a form that, when evaluated at
  2530. load time, should return an object that is equivalent to the argument. The
  2531. exact meaning of ``equivalent'' depends on the type of object and is up to the
  2532. programmer who defines a method for make-load-form. This allows the user to
  2533. program the notion of ``similar as a constant'' (see section 25.1).
  2534.  
  2535. The second value, called the initialization form, is a form that, when
  2536. evaluated at load time, should perform further initialization of the object.
  2537. The value returned by the initialization form is ignored. If the make-load-form
  2538. method returns only one value, the initialization form is nil, which has no
  2539. effect. If the object used as the argument to make-load-form appears as a
  2540. constant in the initialization form, at load time it will be replaced by the
  2541. equivalent object constructed by the creation form; this is how the further
  2542. initialization gains access to the object.
  2543.  
  2544. Two values are returned so that circular structures may be handled. The order
  2545. of evaluation rules discussed below for creation and initialization forms
  2546. eliminates the possibility of partially initialized objects in the absence of
  2547. circular structures and reduces the possibility to a minimum in the presence of
  2548. circular structures. This allows nodes in non-circular structures to be built
  2549. out of fully initialized subparts.
  2550.  
  2551. Both the creation form and the initialization form can contain references to
  2552. objects of user-defined types (defined precisely below). However, there must
  2553. not be any circular dependencies in creation forms. An example of a circular
  2554. dependency: the creation form for the object X contains a reference to the
  2555. object Y, and the creation form for the object Y contains a reference to the
  2556. object X. A simpler example: the creation form for the object X contains a
  2557. reference to X itself. Initialization forms are not subject to any restriction
  2558. against circular dependencies, which is the entire reason for having
  2559. initialization forms. See the example of circular data structures below.
  2560.  
  2561. The creation form for an object is always evaluated before the initialization
  2562. form for that object. When either the creation form or the initialization form
  2563. refers to other objects of user-defined types that have not been referenced
  2564. earlier in the compile-file, the compiler collects all of the creation and
  2565. initialization forms. Each initialization form is evaluated as soon as possible
  2566. after its creation form, as determined by data flow. If the initialization form
  2567. for an object does not refer to any other objects of user-defined types that
  2568. have not been referenced earlier in the compile-file, the initialization form
  2569. is evaluated immediately after the creation form. If a creation or
  2570. initialization form F references other objects of user-defined types that have
  2571. not been referenced earlier in the compile-file, the creation forms for those
  2572. other objects are evaluated before F and the initialization forms for those
  2573. other objects are also evaluated before F whenever they do not depend on the
  2574. object created or initialized by F. Where the above rules do not uniquely
  2575. determine an order of evaluation, it is unspecified which of the possible
  2576. orders of evaluation is chosen.
  2577.  
  2578. While these creation and initialization forms are being evaluated, the objects
  2579. are possibly in an uninitialized state, analogous to the state of an object
  2580. between the time it has been created by allocate-instance and it has been
  2581. processed fully by initialize-instance. Programmers writing methods for
  2582. make-load-form must take care in manipulating objects not to depend on slots
  2583. that have not yet been initialized.
  2584.  
  2585. It is unspecified whether load calls eval on the forms or does some other
  2586. operation that has an equivalent effect. For example, the forms might be
  2587. translated into different but equivalent forms and then evaluated; they might
  2588. be compiled and the resulting functions called by load (after they themselves
  2589. have been loaded); or they might be interpreted by a special-purpose
  2590. interpreter different from eval. All that is required is that the effect be
  2591. equivalent to evaluating the forms.
  2592.  
  2593. It is valid for user programs to call make-load-form in circumstances other
  2594. than compilation, providing the argument's metaclass is not built-in-class or a
  2595. subclass of built-in-class.
  2596.  
  2597. Applying make-load-form to an object whose metaclass is standard-class or
  2598. structure-class for which no user-defined method is applicable signals an
  2599. error. It is valid to implement this either by defining default methods for the
  2600. classes standard-object and structure-object that signal an error or by having
  2601. no applicable method for those classes.
  2602.  
  2603. See load-time-eval.
  2604.  
  2605. In the following example, an equivalent instance of my-class is reconstructed
  2606. by using the values of two of its slots. The value of the third slot is derived
  2607. from those two values.
  2608.  
  2609. (defclass my-class ()
  2610.   ((a :initarg :a :reader my-a)
  2611.    (b :initarg :b :reader my-b)
  2612.    (c :accessor my-c)))
  2613.  
  2614. (defmethod shared-initialize ((self my-class) slots &rest inits)
  2615.   (declare (ignore slots inits))
  2616.   (unless (slot-boundp self 'c)
  2617.     (setf (my-c self)
  2618.           (some-computation (my-a self) (my-b self)))))
  2619.  
  2620. (defmethod make-load-form ((self my-class))
  2621.   `(make-instance ',(class-name (class-of self))
  2622.                   :a ',(my-a self) :b ',(my-b self)))
  2623.  
  2624. This code will fail if either of the first two slots of some instance of
  2625. my-class contains the instance itself. Another way to write the last form in
  2626. the preceding example is
  2627.  
  2628. (defmethod make-load-form ((self my-class))
  2629.   (make-load-form-saving-slots self '(a b)))
  2630.  
  2631. This has the advantages of conciseness and handling circularities correctly.
  2632.  
  2633. In the next example, instances of class my-frob are ``interned'' in some way.
  2634. An equivalent instance is reconstructed by using the value of the name slot as
  2635. a key for searching for existing objects. In this case the programmer has
  2636. chosen to create a new object if no existing object is found; an alternative
  2637. possibility would be to signal an error in that case.
  2638.  
  2639. (defclass my-frob ()
  2640.   ((name :initarg :name :reader my-name)))
  2641.  
  2642. (defmethod make-load-form ((self my-frob))
  2643.   `(find-my-frob ',(my-name self) :if-does-not-exist :create))
  2644.  
  2645. In the following example, the data structure to be dumped is circular, because
  2646. each node of a tree has a list of its children and each child has a reference
  2647. back to its parent.
  2648.  
  2649. (defclass tree-with-parent () ((parent :accessor tree-parent)
  2650.                                (children :initarg :children)))
  2651.  
  2652. (defmethod make-load-form ((x tree-with-parent))
  2653.   (values
  2654.     `(make-instance ',(class-of x)
  2655.                     :children ',(slot-value x 'children))
  2656.     `(setf (tree-parent ',x) ',(slot-value x 'parent))))
  2657.  
  2658. Suppose make-load-form is called on one object in such a structure. The
  2659. creation form creates an equivalent object and fills in the children slot,
  2660. which forces creation of equivalent objects for all of its children,
  2661. grandchildren, etc. At this point none of the parent slots have been filled in.
  2662. The initialization form fills in the parent slot, which forces creation of an
  2663. equivalent object for the parent if it was not already created. Thus the entire
  2664. tree is recreated at load time. At compile time, make-load-form is called once
  2665. for each object in the tree. All the creation forms are evaluated, in
  2666. unspecified order, and then all the initialization forms are evaluated, also in
  2667. unspecified order.
  2668.  
  2669. In this final example, the data structure to be dumped has no special
  2670. properties and an equivalent structure can be reconstructed simply by
  2671. reconstructing the slots' contents.
  2672.  
  2673. (defstruct my-struct a b c)
  2674.  
  2675. (defmethod make-load-form ((s my-struct))
  2676.   (make-load-form-saving-slots s))
  2677.  
  2678. This is easy to code using make-load-form-saving-slots.
  2679.  
  2680. [Function]
  2681. make-load-form-saving-slots object &optional slots
  2682.  
  2683. This returns two values suitable for return from a make-load-form method. The
  2684. first argument is the object. The optional second argument is a list of the
  2685. names of slots to preserve; it defaults to all of the local slots.
  2686.  
  2687. make-load-form-saving-slots returns forms that construct an equivalent object
  2688. using make-instance and setf of slot-value for slots with values, or
  2689. slot-makunbound for slots without values, or other functions of equivalent
  2690. effect.
  2691.  
  2692. Because make-load-form-saving-slots returns two values, it can deal with
  2693. circular structures; it works for any object of metaclass standard-class or
  2694. structure-class. Whether the result is useful depends on whether the object's
  2695. type and slot contents fully capture an application's idea of the object's
  2696. state.
  2697. [change_end]
  2698.  
  2699. -------------------------------------------------------------------------------
  2700.  
  2701. 23.5. Accessing Directories
  2702.  
  2703. The following function is a very simple portable primitive for examining a
  2704. directory. Most file systems can support much more powerful directory-searching
  2705. primitives, but no two are alike. It is expected that most implementations of
  2706. Common Lisp will extend the directory function or provide more powerful
  2707. primitives.
  2708.  
  2709. [Function]
  2710. directory pathname &key
  2711.  
  2712. A list of pathnames is returned, one for each file in the file system that
  2713. matches the given pathname. (The pathname argument may be a pathname, a string,
  2714. or a stream associated with a file.) For a file that matches, the truename
  2715. appears in the result list. If no file matches the pathname, it is not an
  2716. error; directory simply returns nil, the list of no results. Keywords such as
  2717. :wild and :newest may be used in pathname to indicate the search space.
  2718.  
  2719. [change_begin]
  2720. X3J13 voted in March 1988 (PATHNAME-STREAM)   to specify exactly which streams
  2721. may be used as pathnames. See section 23.1.6.
  2722.  
  2723. X3J13 voted in January 1989 (CLOSED-STREAM-OPERATIONS)   to specify that
  2724. directory is unaffected by whether the first argument, if a stream, is open or
  2725. closed. If the first argument is a stream, directory behaves as if the function
  2726. pathname were applied to the stream and the resulting pathname used instead.
  2727. However, X3J13 commented that the treatment of open streams may differ
  2728. considerably from one implementation to another; for example, in some operating
  2729. systems open files are written under a temporary or invisible name and later
  2730. renamed when closed. In general, programmers writing code intended to be
  2731. portable should be careful when using directory.
  2732.  
  2733. X3J13 voted in June 1989 (PATHNAME-LOGICAL)   to require directory to accept
  2734. logical pathnames (see section 23.1.5). However, the result returned by
  2735. directory never contains a logical pathname.
  2736. [change_end]
  2737.  
  2738. -------------------------------------------------------------------------------
  2739. Implementation note: It is anticipated that an implementation may need to
  2740. provide additional parameters to control the directory search. Therefore
  2741. directory is specified to take additional keyword arguments so that
  2742. implementations may experiment with extensions, even though no particular
  2743. keywords are specified here.
  2744.  
  2745. As a simple example of such an extension, for a file system that supports the
  2746. notion of cross-directory file links, a keyword argument :links might, if
  2747. non-nil, specify that such links be included in the result list.
  2748. -------------------------------------------------------------------------------
  2749.  
  2750. -------------------------------------------------------------------------------
  2751.  
  2752.  
  2753.  
  2754.  
  2755.  
  2756.  
  2757.